Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

ruby on rails - Country expected, got String error

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"=>"a?“",
 "league"=>{"league_short"=>"CL",
 "country"=>"England",
 "level"=>"2",
 "league"=>"The Championship"}}

But then I get this error message:

Country expected, got String

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use :country_id instead of :country

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...