Hello my fellow programmers!
Noob here with a noob question. I just began ruby on rails 3 weeks ago. So its save to say that i am fierily new with it. I am building a order management system that contains 2 tables. :customer and :order. I can destroy, show, and possibly edit. But i can only create for customer and not for order that comes with it. This is the code that i have so far
class Order < ActiveRecord::Base
belongs_to :customer
end
class Customer < ActiveRecord::Base
has_many :orders
end
def new
@orders = Order.new
@customer = Customer.new ({:voornaam => "ABC"})
end
def create
@customer = Customer.new(customer_params)
if @customer.save
orders = Order.where(params(order_params))
@customer.orders << orders
flash[:notice] = "Subject created successfully"
redirect_to(:action => 'index')
else
render('new')
end
end
def customer_params
#same as using "params[:subject]", expect that it:
# - raises an error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:customer).permit(:voornaam, :achternaam)
end
private
def order_params
#same as using "params[:subject]", expect that it:
# - raises an error if :subject is not present
# - allows listed attributes to be mass-assigned
params.require(:customer).permit(:pakket, :datum_bestelt, :verstuurt, :datum_verstuurt, :tweede_exemplaar, :packettracer_ID)
end
end
Create Order
<%= form_for(:customer, :url=> {:action => 'create'}) do |f| %>
<table summary="subject form fields">
<tr>
<th>Voornaam</th>
<td><%= f.text_field(:voornaam) %></td>
</tr>
<tr>
<th>Achternaam</th>
<td><%= f.text_field(:achternaam) %></td>
</tr>
<%= f.fields_for :order do |s| %>
<tr>
<th>pakket</th>
<td><%= s.text_field(:pakket) %></td>
</tr>
<tr>
<th>datum_bestelt</th>
<td><%= s.text_field(:datum_bestelt) %></td>
</tr>
<tr>
<th>verstuurt</th>
<td><%= s.text_field(:verstuurt) %></td>
</tr>
<tr>
<th>datum_verstuurt</th>
<td><%= s.text_field(:datum_verstuurt) %></td>
</tr>
<tr>
<th>tweede_exemplaar</th>
<td><%= s.text_field(:tweede_exemplaar) %></td>
</tr>
<tr>
<th>packettracer_ID</th>
<td><%= s.text_field(:packettracer_ID) %></td>
</tr>
<% end %>
</table>
Aucun commentaire:
Enregistrer un commentaire