mrudult
mrudult

Reputation: 2570

Rails updating records via nested model forms and delete links

I have two models- Family and Person:(Using Mongoid and Rails 3.2.13)

family.rb

attr_accessible :location  
has_many :persons
accepts_nested_attributes_for :persons  

person.rb

attr_accessible :name
belongs_to :family  

In the FamiliesController I have:

  def edit
   @family=Family.find(params[:id])
  end

  def update
    @family=Family.find(params[:id])
    @family.update_attributes(params[:family])
  end  

in the edit.html.erb for families controller:

<div class="container">
<%= simple_form_for @family do |f| %>
  <%= f.error_messages %>
    <%= f.input :location %>
    <%= f.simple_fields_for :persons do |p| %>
      <%= p.input :name %>
    <%end%>
  <%= f.submit "Submit" %>
<% end %>
</div>  

But it only updates the family attributes and the persons attrubutes remains the same.

How do I update the Person's attributes as well?

Also I want to add a delete button for each person which will delete the corresponding person. How to achieve that?

Upvotes: 0

Views: 105

Answers (1)

Oleg Haidul
Oleg Haidul

Reputation: 3732

Try to add persons_attributes in attr_accessible # In family.rb

Upvotes: 1

Related Questions