3

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 
Carla Dessi
  • 9,086
  • 9
  • 39
  • 53
  • 2
    Have you tried to loose the "" around `@partcode`? – Gerry Jul 04 '12 at 11:37
  • yup, then it comes up with: undefined method `model_name' for NilClass:Class – Carla Dessi Jul 04 '12 at 11:38
  • 2
    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 Jul 04 '12 at 11:40
  • 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 Jul 04 '12 at 11:44
  • 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 Jul 04 '12 at 11:46
  • `@partcode = GoodsIn.partcodes.build` on your controller. – MurifoX Jul 04 '12 at 11:46
  • yeh already has that in the class definition, its a one-to-many relationship. if i put `@partcode = GoodsIn.partcodes.build` in my goods in controller i get this error: `undefined method 'partcodes' for #` – Carla Dessi Jul 04 '12 at 11:54

4 Answers4

7

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

Hishalv
  • 3,052
  • 3
  • 29
  • 52
0

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"])
Trip
  • 26,756
  • 46
  • 158
  • 277
  • tried that and i get this: `undefined local variable or method 'params' for GoodsInsController:Class` – Carla Dessi Jul 04 '12 at 11:57
  • Ah ok, throw in a debugger right after `def create` . What is being returned after you submit? – Trip Jul 04 '12 at 11:58
0

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|%> 
MurifoX
  • 14,991
  • 3
  • 36
  • 60
0

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.

Draiken
  • 3,805
  • 2
  • 30
  • 48
  • please see my updated answer with my code from both models :) – Carla Dessi Jul 04 '12 at 12:09
  • @CarlaDessi what is being inserted in the `@partcode` variable? – Draiken Jul 04 '12 at 12:12
  • um.. what ever is typed into the text field in the form? sorry if that's not what you mean.. – Carla Dessi Jul 04 '12 at 12:13
  • @CarlaDessi the classes seem to be setup correctly, so the error seems to be from what is in the `@partcode` instance variable. Please post the controller code that sets `@partcode`, it is probably not being initialized with `GoodsIn.partcodes.build` – Draiken Jul 04 '12 at 12:20
  • my partcode controller has quite a lot in it, but i'm guessing you need this bit: `@partcode = GoodsIn.build_partcode def partcode @partcode = Partcode.all respond_to do |format| format.html # index.html.erb format.json { render :json => @partcode }` – Carla Dessi Jul 04 '12 at 12:23
  • also i already have this in my controller `@partcode = Partcode.new` – Carla Dessi Jul 04 '12 at 12:25
  • @CarlaDessi please update your answer with the `new` action of your controller, and possibly with the stack trace of the error your receive. Without all the parts of the problem, we're just guessing here. – Draiken Jul 04 '12 at 12:43