GIST is here:
https://gist.github.com/1553371
These are the models I have:
class Character < ActiveRecord::Base
has_many :weapons
end
class Weapon < ActiveRecord::Base
belongs_to :character
end
and this is what my view in HAML looks like:
= form_for(@weapon) do |f|
%ul.fields
%li
= f.label :character
= collection_select :weapon, :character_id, Character.all, :id, :name, {:prompt => 'Please Select a Character.'}
but I keep getting this error:
undefined method `character_id' for #<Weapon:0x007f9033232088>
I’ve attempted using a string for character_id, that also didn’t work. The form renders with :character, but then of course it won’t save back since it needs the id.
Your code looks fine, it’s likely that you don’t have a
character_idfield on your weapon model. If you do then you have pending migrations that need to be run.You can check the current state of your database by looking at
db/schema.dbit will show if you have the character_id column or not.You can run your migrations using
rake db:migratethen userake db:test:prepareto update your test database as well.If it’s not working after running migrations then you’ll need to create one, should look something like: