the_
the_

Reputation: 1173

Rails 4 nested form issue

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!

Upvotes: 3

Views: 3158

Answers (2)

d34n5
d34n5

Reputation: 1318

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.

Upvotes: 5

1andsock
1andsock

Reputation: 1567

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

Upvotes: 5

Related Questions