3

Hi I'm using the nested form plugin and trying to make it work for rails 4 instead of rails 3. Basically my model looks like this:

has_many :item, :dependent => :destroy

accepts_nested_attributes_for :item, :reject_if => lambda { |a| a[:item].blank? }, :allow_destroy => true

and my view looks like this:

<%= nested_form_for(@store) do |f| %>
  <%= f.fields_for :item do |item_form| %>
     <%= item_form.text_field :name %>
     <%= item_form.link_to_remove "Remove this item" %>
  <% end %>
<% end %>

this works (in terms of presentation - you can add and delete fields like you should be able to) but doesn't save the item names.

I tried this in my controller (these are the protected attributes/params):

def store_params
  params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes)
end

but it still comes up with:

Unpermitted parameters: item_attributes

Thanks for all help!

3
  • You will probably have to allow the fields of item as well. Commented Jul 10, 2013 at 0:54
  • I'm not sure if this is related to your problem, but the nested_form gem does not explicitly support Rails 4. Commented Jul 10, 2013 at 0:56
  • Try cocoon gem instead. Seems to be a good way to go with Rails 4.0 because it's a bit more updated. Commented Sep 9, 2013 at 10:57

2 Answers 2

5

You're going to have to permit the fields of item (like name) to be allowed as well.

So try this:

def store_params
    params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes: [:name])
end
Sign up to request clarification or add additional context in comments.

Comments

5

sometimes you have to specify the :id like this:

def store_params
    params.require(:store).permit(:name, :owner, :description,:url, :user, item_attributes: [:id, :name])
end

in a similar case I had last week, not specifying the :id made Rails 4 create an new entity instead of updating the existing one.

1 Comment

This. Adding :id fixed it so that I wasn't multiplying the list on every submit. I also had to add :_destroy if I wanted my nested form to delete properly. I wonder if there's some way to whitelist the entire _attributes without resorting to permit!

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.