form for - Rails 4, show attributes -
if want submit fields of nested resources instead of parent. how can this?
i've got 2 models: product , product_details.
=form_for @product |f| =f.fields_for product_details |ff| =ff.radio_button :price, ff.price =f.submit
so need submit form above product_details_controller instead of products_controller.
if form_form product_details got "to_key" undefined..
what best way this?
accepts_nested_attributes_for can you.
for example:-
class project < activerecord::base has_many :tasks accept_nested_attributes_for :tasks, :allow_destroy => true end
in view
<% form_for @project |project_form| %> <% project_form.fields_for :tasks |task_form| %> <p> <div> <%= task_form.label :name, 'task:' %> <%= task_form.text_field :name %> </div> </p> <%end%> <%= project_form.submit %> <% end %>
so @project.save save child tasks too.also can modify controller code , way want
try on rails console
>> project = project.first => #<project id: 1, name: "nested models patches", created_at: "2009-01-22 11:17:15", updated_at: "2009-01-22 11:17:15", author_id: 1> >> project.tasks => [#<task id: 1, project_id: 1, name: "write 'em", due_at: nil, created_at: "2009-01-22 11:17:15", updated_at: "2009-01-22 11:17:15">, #<task id: 2, project_id: 1, name: "test 'em", due_at: nil, created_at: "2009-01-22 11:17:15", updated_at: "2009-01-22 11:17:15">, #<task id: 3, project_id: 1, name: "create demo app", due_at: nil, created_at: "2009-01-22 11:17:15", updated_at: "2009-01-22 11:17:15">, #<task id: 4, project_id: 1, name: "scrutinize", due_at: nil, created_at: "2009-01-22 11:17:15", updated_at: "2009-01-22 11:17:15">] >> project.tasks.second.name = "" => "" >> project.valid? => false
Comments
Post a Comment