ruby on rails - Complex slightly-nested form -


i'm trying make somewhat-complex somewhat-nest form. have (simplified) following:

models/product.rb

class product < activerecord::base   attr_accessible :name   has_many :colors end 

models/color.rb

class color < activerecord::base   attr_accessible :quantity   belongs_to :product end 

views/admin/update_inventories.html.erb

<% @products.each |p| %>   <%= p.name %>   <% p.colors.each |c| %>     <%= c.name %>:      <%= form_for :color, :url => color_update_path(:id => v.id) |f| %>        <%= f.number_field :quantity, :value => c.quantity, :min => 0 %>        <%= submit_tag "update" %>     <% end %>   <% end %> <% end %> 

the problem creates frightning amount of forms , have update 1 suit 1 suit. want view:

view/admin/update_inventories_revised.html.erb

<%= form_for :inventory, :url => custom_update_path |f| %>   <% @products.each |p| %>     <%= p.name %>     <% p.colors.each |c| %>       <%= f.number_field :quantity, :value => c.quantity, :min => 0 %>     <% end %>   <% end %>   <% f.submit_tag "update all" %> <% end %> 

but can't seem figure out controller side logic match it. need controller side make view (or same effect) function?

you need setup accepts_nested_attributes_for in model.

when setup, need product = product.create!(params[:product]) nested objects save automatically, assuming nested forms built right.

if need building nested forms, check out fields_for @ this link.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -