0

I've been puzzling over this for quite some time now and can't figure it out.

I've got 2 models:

class Vehicle < ActiveRecord::Base
  attr_accessible :year, :capacity,
  :size, :body, :model_id, :maker_id, :parameters_attributes

    validates :year, numericality: { greater_than: 1900 }
    validates :year, :capacity, :size, :body, presence: true

    belongs_to :model
    belongs_to :maker
    has_many :parameters
    accepts_nested_attributes_for :parameters
    end

and

class Parameter < ActiveRecord::Base
  attr_accessible :tag, :value
  validates :tag, :value, presence: true
  belongs_to :vehicle
end

in new vehicle view i've got:

= form_for [:admin, @vehicle], html: { multipart: true } do |f|
    =# some other stuff in between
    = f.text_field :value, size: 4

I get this error

undefined method `value'

Just can't seem to get it working. Help, anyone?

EDIT routes.rb

resources :vehicles 
resources :parameters

resources :makers do
  resources :models
end

3 Answers 3

1

If you are using nested form, you should have something like

f.fields_for :parameters do |parameter|

and than:

parameter.text_field :value, size: 4

Also, remember to create the some parameters in the controller, for example:

def new
  @vehicle = Vehicle.new
  2.times { @vehicle.parameters.build } #it will create 2 parameters
...
end
0

f refers to @vehicle, it seems only Parameter bears this field. That's why it fails.

Sidenotes:

  • In Vehicle you have accepts_nested_attributes_for :parameters but you don't have parameters_attributes in the attr_accessible, can't be good.

  • If you want to call the relationship in the form consider using fields_for

5
  • I've tried this, no longer have undefined error, but the text_field does not appear. I'm not so sure if 'accepts_nested_attributes_for` is the way, 'cause my parameters aren't nested within vehicles. I'll edit my original post, with routes.rb and changes I've made in models. Feb 28, 2013 at 13:00
  • routes don't have any role here Feb 28, 2013 at 13:03
  • use fields_for as I told you. If you have nothing it means you didn't build any object. Do @vehicle.parameters.build in your controller Feb 28, 2013 at 13:04
  • I am using fields_for. I did @vehicle.parameters.build in new method. I got undefined method `parameters' Feb 28, 2013 at 13:23
  • seems you relation is unknown to rails... did you try to reboot the server? Feb 28, 2013 at 13:30
-1

Ok, I've made a mess of things.

Firstly I've been trying to

def new
  @vehicle = @vehicle.parameters.build
end

hence the error undefined method. After a while I got to the correct syntax, which is the one gabrielhilal added after a while.

def new
  @vehicle = Vehicle.new
  @vehicle.parameters.build
end

No matter ;) Still had problems, because after clicking "create" he wouldn't add records in the database. Turned out that I've set the validates presence: true for tag, but didn't assign any value to it. After fixing that, it worked like a charm. Thanks a lot for all the help.

On to the next puzzle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.