预期国家/地区,出现字符串错误

问题描述:

我有两个模型国家"和联盟",国家有很多联盟,联盟属于国家.添加联赛时,我有一个包含国家/地区的列表框,提交表单后,将发送实际国家/地区:

I have 2 models "Country" and "League", Country has many Leagues and League belongs to Country. When adding a league, I have a listbox with countries and when the form is submitted, the actual country is send:

{"commit"=>"Create League",
 "authenticity_token"=>"wuAuj5vowkk2R56TuFkWE8J3x3vue5RbnNPcbpjuG3Q=",
 "utf8"=>"âœ"",
 "league"=>{"league_short"=>"CL",
 "country"=>"England",
 "level"=>"2",
 "league"=>"The Championship"}}

但后来我收到此错误消息:

But then I get this error message:

Country expected, got String

在 Country 模型中,country_id(整数)和 country(字符串)作为字段,在 League 模型中,country 作为字符串字段.在 League 控制器中,我有这个来填充下拉列表:@countries = Country.dropdown_list.在联盟/新视图中,我有这个选择字段:.出了什么问题?

In the Country model I have country_id (integer) and country (string) as fields, in the League model I have country as a string field. In the League controller I have this to pouplate the dropdown: @countries = Country.dropdown_list. In the league/new view I have this select field: <%= f.select :country, @countries %>. What is going wrong?

您需要在该请求中发送 country_id(这是主键)而不是名称England".关系与主键相关联.

You need to send country_id (which is the primary key) instead of name 'England' in that request. The relationships are associated with the primary keys.

<%= f.select :country, Country.all.collect {|c| [ c.name, c.id ] } %>