我有两个模型“国家”和“联盟”,国家有许多联盟和联盟属于国家。在添加联盟时,我有一个包含国家的列表框,当表单提交时,实际的国家是发送:
{"commit"=>"Create League",
"authenticity_token"=>"wuAuj5vowkk2R56TuFkWE8J3x3vue5RbnNPcbpjuG3Q=",
"utf8"=>"✓",
"league"=>{"league_short"=>"CL",
"country"=>"England",
"level"=>"2",
"league"=>"The Championship"}}但是后来我收到了这个错误消息:
Country expected, got String在Country模型中,我使用country_id (整数)和country (字符串)作为字段,在联盟模型中,我使用country作为字符串字段。在联盟控制器中,我有这个下拉列表:@countries = Country.dropdown_list。在联盟/新视图中,我有一个选择字段:<%= f.select :country, @countries %>。哪里出了问题?
发布于 2011-04-19 22:50:58
您需要在该请求中发送country_id (这是主键),而不是名称'England‘。这些关系与主键相关联。
<%= f.select :country, Country.all.collect {|c| [ c.name, c.id ] } %>发布于 2013-05-01 09:05:41
您需要使用:country_id而不是:country
<%= f.select :country_id, Country.all.collect {|c| [c.name, c.id]} %>发布于 2011-05-11 00:08:36
我得到了这个错误:
Artist (#xxx) expected, got String (#xxx)这就是我在Rails 3.0.x中修复它的方式:
class OtherModel
belongs_to :artist
validates :artist, :presence => true
#...
end
<%= form_for_other.collection_select :artist_id,
Artist.all, :id, :name,
:prompt => true %>因此,当我将collection_select输入上的方法设置为外键而不是模型名称时,它可以正常工作
https://stackoverflow.com/questions/5716412
复制相似问题