3
votes

This is the relevant part of my nested form:

<div class="field">
<%= f.fields_for "@partcode" do |p|%> 

  <%= p.label "partcode"%><br />
  <%= p.text_field :partcode %>

<% end %>
</div>

and i already have this in my model:

attr_accessible :partcode,
                :description

yet when i enter something in to the form, i get this error:

Can't mass-assign protected attributes: @partcode

Here is my partcode model:

class Partcode < ActiveRecord::Base
 attr_accessible :partcode,
              :description

  validates       :partcode,
              :description,
              :presence => true

 belongs_to "goods_ins"

 accepts_nested_attributes_for "goods_ins"


end

and here is all the code from my goods in model:

class GoodsIn < ActiveRecord::Base
  attr_accessible :c4lpono, 
              :courier, 
              :deliverydate,  
              :deliverynoteno,  
              :description,  
              :destination,  
              :notes,  
              :partcode,  
              :quantity,  
              :signedby,  
              :suppliername

  validates       :deliverydate,  
              :deliverynoteno,  

              :destination,

              :quantity,  
              :signedby,  
              :suppliername,
              :presence => true

 has_many :suppliers

 has_many :partcodes

 accepts_nested_attributes_for :partcodes
end

Also here is the new part of my partcode controller:

def new
@partcode = Partcode.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render :json => @partcode }
end
end 
4
Have you tried to loose the "" around @partcode? - Gerry
yup, then it comes up with: undefined method `model_name' for NilClass:Class - Carla Dessi
Then you haven't instantiated the @partcode inside your controller. Try writing inside your controller action: @partcode = Partcode.new (or whatever the class is). - Gerry
that's already in my controller.. i forgot to explain in more depth, the form is for a table called goods_in, but this nested part is for a table called partcode.. - Carla Dessi
Ok..The GoodsIn model has the following declaration inside it's class definition? accepts_nested_attributes_for :partcode. Also is this a one-to-one relationship or one-to-many? - Gerry

4 Answers

7
votes

Should'nt you have :partcode_attributes to attr_accessible(in GoodsIn Model), like this:

attr_accessible :partcode_attributes

Assuming your model association is configured that way. hope it helps

0
votes

I'd like to look at your model. But attr_accessible isn't an instance. I think it should just say this

<%= f.fields_for "partcode" do |p|%>` 

than in your controller :

@partcode = Partcode.new(params["partcode"])
0
votes

Looking at the comments from your answer i believe you can use the build method made for has_one relanshionships:

@partcode = GoodsIn.build_partcode

SO your form can be like this:

<%= f.fields_for @partcode do |p|%> 
0
votes

Your model has to have accepts_nested_attributes_for :partcode or it won't accept it's attributes.

Also Partcode class has to have his attributes white listed too.

UPDATE:

The @partcode must have a new Partcode in it. Make sure you set @partcode = @goods_in.partcodes.build or a simple @partcode = Partcode.new.

The fields for on the form should be like this <%= f.fields_for @partcode do |p| %>, without the quotes too.