Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"=>"✓",
 "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?

like image 444
John Avatar asked Apr 19 '11 12:04

John


1 Answers

You need to use :country_id instead of :country

<%= f.select :country_id, Country.all.collect {|c| [c.name, c.id]} %>
like image 194
Jonathan Roy Avatar answered Oct 04 '22 11:10

Jonathan Roy