1
votes

I've used the nested_form gem, whenever i try and submit something to my form i get the Can't mass-assign protected attributes:items message, even though i've already put attr_accessible in my models.

Form:

<%= nested_form_for(@goods_in) do |f| %>
...

<%= f.fields_for :items do |i| %>
<td><%= i.text_field :description, :autocomplete => :off%></td>
<td><%= i.text_field :quantity, :autocomplete => :off %></td>
<th><%= i.link_to_remove "Remove this item" %></th>
<% end %>
<%= f.submit :"Submit Delivery" %>
<% end %>

Goods In Model:class GoodsIn < ActiveRecord::Base belongs_to :supplier has_many :items

attr_accessible :c4lpono, 
              :courier, 
              :deliverydate,  
              :deliverynoteno,  
              :destination,  
              :notes,  
              :quantity,  
              :signedby,
              :supplier_id,
              :partcode_ids

accepts_nested_attributes_for :supplier

validates :c4lpono, 
              :deliverydate,  
              :deliverynoteno,  
              :destination,  
              :quantity,  
              :signedby,
              :presence =>true                  

end

Item Model

class Item < ActiveRecord::Base
belongs_to :goods_in

attr_accessible :quantity,
              :partcode,
              :description,
              :goods_in_id


accepts_nested_attributes_for :goods_in


end

Goods In Controller:

def create
@goods_in = GoodsIn.new(params[:goods_in])
end
3
can you show the relevant controller code where you're calling new, create, or update_attributes ? - Ben Taitelbaum
Added it to my question! - Carla Dessi

3 Answers

2
votes

you have to add

attr_accessible :items_attributes

And here's a link to the documentation :)

0
votes

I think there's an error in your Goods model.

It should read has_many :items instead of has_many :item.

0
votes

It's hard to tell what you are trying to achieve with your models but I think you want the folllowing:

Your GoodsIn needs to have the relationship for accepts_nested_attributes_for :items . The belongs_to and accepts_nested_attributes_for is off.