I cannot save array params obj_product[]
Database
|categories|
|id| |name|
1 HW
2 SF
3 PC
|products|
|id| |amount|
But after saving the table 'PRODUCTS' should save array params will be like this demo
|products|
|id| |amount| |category_id|
1 100 1
2 200 2
3 300 3
Controller:
def new
@categories = Category.all
@obj_product = Product.new(params[:obj_product])
end
def create
obj_product.new(params[:obj_product])
if obj_product.save()
redirect_to :action=>"index"
else
redirect_to :action=>"new"
end
end
View:
<% form_for :obj_product, :url => {:action=>'create'} do |f| %>
<% @categories.each do |category| %>
<%= f.text_field :category_id %>
<%= f.text_field :amount ,:id => "part_#{category.id}", :name => "flow_budget_detail[#{category.id}][amount]" %>
<% end %>
<$ end %>
Log:
INSERT INTO `flow_budget_details` (`category_id`, `amount`) VALUES( 1, 100)
I should expect this log:
INSERT INTO `flow_budget_details` (`category_id`, `amount`) VALUES( 1, 100)
INSERT INTO `flow_budget_details` (`category_id`, `amount`) VALUES( 2, 200)
INSERT INTO `flow_budget_details` (`category_id`, `amount`) VALUES( 3, 300)
I tried this adding the symbol [] but got error The error occurred while evaluating nil.[]:
in new
@obj_product = Product.new(params[:obj_product][])
in create
obj_product.new(params[:obj_product][])
I want save my array in this form. Note: The table categories can be more than 3, I wrote 3 in my example.
Please somebody can help me?
Aucun commentaire:
Enregistrer un commentaire