0

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?

2
  • Are you using params.permit somewhere in the controller? Commented Sep 21, 2013 at 7:44
  • it is used to filter which params are allowed , but if you are not using it then that is not the cause. Commented Sep 21, 2013 at 7:51

1 Answer 1

1

Try to add persons_attributes in attr_accessible # In family.rb

Sign up to request clarification or add additional context in comments.

5 Comments

I have that accepts_nested_attributes_for :persons then why do I need this?
Yeah that worked like charm. How about adding a seperate delete button for each person? See questions last line. Will adding a simple delete button for each person do the work?
B/c accepts_nested_attributes_for :persons only generate extra params for your nested model.
I think, delete button will be inside persons_attributes. It will work well.
Update Well I found a solution here. But that gives me No input found for checkbox error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.