Country expected, got String error

后端 未结 6 1204
栀梦
栀梦 2020-12-10 16:38

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 i

相关标签:
6条回答
  • 2020-12-10 17:14

    I was getting this error:

    Country (#xxx) expected, got String (#xxx) This was how I fixed it in Rails 3.0.x:

    <%= f.collection_select :country_id, Country.all.collect,  :id, :name %>
    
    0 讨论(0)
  • 2020-12-10 17:15

    for others who met the same problem:

    this error is caused when you have two fields in your form like:

    video: 'some string'
    video['url']:  'some url'
    

    then rails will crash with the error: expected Hash (got String) for param

    the solution is quite simple: change 'video' to something else. e.g.:

    video_origin_url: 'some string'
    video['url']: 'some url'
    
    0 讨论(0)
  • 2020-12-10 17:16

    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 ] } %>
    
    0 讨论(0)
  • 2020-12-10 17:16

    I was getting this error:

    Artist (#xxx) expected, got String (#xxx)
    

    This was how I fixed it in 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 %>
    

    So it worked when I set the method on the collection_select input to the foreign key instead of the model name

    0 讨论(0)
  • 2020-12-10 17:22

    The League model should reference country by its id ( country_id ) and not the string.

    0 讨论(0)
  • 2020-12-10 17:32

    You need to use :country_id instead of :country

    <%= f.select :country_id, Country.all.collect {|c| [c.name, c.id]} %>
    
    0 讨论(0)
提交回复
热议问题