gpt4 book ai didi

ruby-on-rails - collection_select 给出 ActiveRecord::AssociationTypeMismatch

转载 作者:行者123 更新时间:2023-12-03 22:24:13 24 4
gpt4 key购买 nike

我不确定我问的这个问题是否正确,或者只是太累了,以至于不明显。

我的代码是:

_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 

把所有东西都换过来了,它工作得很完美——我想我太累了:)

最佳答案

你应该改变:

f.collection_select :category, Category.where(:user_id => current_user.id), :id, :name

到:

f.collection_select :category_id, Category.where(:user_id => current_user.id), :id, :name

关于ruby-on-rails - collection_select 给出 ActiveRecord::AssociationTypeMismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com