私がこの質問を正しくしているのか、それとも疲れすぎてはっきりしないのかはわかりません。
私のコードは次のとおりです。
_form.html.haml
= f.collection_select :category, Category.where(:user_id => current_user.id), :id, :name
私が得るものは
ActiveRecord::AssociationTypeMismatch in LinksController#create
Category(#70203963939880) expected, got String(#70203929255820)
ログは
Started POST "/links" for 127.0.0.1 at 2012-11-12 22:14:05 -0800
Processing by LinksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"z4RYcUW3hGLfKBAwQLMZbye2I1mn16fg6BSBGe7GILU=", "link"=>{"name"=>"SFGate", "url"=>"http://www.sfgate.com/", "category"=>"1"}, "commit"=>"Save"}
Completed 500 Internal Server Error in 0ms
:idを整数ではなく文字列として渡すように見えます。手がかりはありますか?
アップデート:
class Category < ActiveRecord::Base
belongs_to :user
has_many :links
attr_accessible :name, :color, :position
COLOR_CODES = ["Green", "Blue", "Yellow", "Orange", "Red", "Purple", "Pink"]
end
class Link < ActiveRecord::Base
belongs_to :user
belongs_to :category
attr_accessible :category, :name, :position, :url, :user_id
end
修正:私は持っていた
t.integer :category
それ以外の
t.integer :category_id
すべてを切り替えて、完璧に機能しました-私は疲れすぎたと思います:)