预期国家/地区,出现字符串错误 [英] Country expected, got String error

查看:41
本文介绍了预期国家/地区,出现字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.在联盟/新视图中,我有这个选择字段:<%= f.select :country, @countries %>.出了什么问题?

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 ] } %>

这篇关于预期国家/地区,出现字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆