0

I have searched everywhere for this, and can't seem to find an answer.

I am trying to set the default value of my Language dropdown to English.

<%= f.collection_select(:native_language, Language.order('language ASC').all, :language, :language, :selected => [Language.find_by(:language => "English")] ) %>

What am I doing wrong?

gwalshington
  • 1,418
  • 2
  • 30
  • 60

1 Answers1

0

You can use select or collection_select:

<%= f.collection_select(:native_language, Language.order('language ASC').all, :language, :language, {:selected => Language.find_by(:language => "English").language}  %>

OR

<%= f.select :native_language, options_for_select(Language.order('language ASC').all, Language.find_by(:language => "English").language) %>
Hasmukh Rathod
  • 1,108
  • 1
  • 14
  • 20
  • both return 'undefined method `map' for #' – gwalshington Aug 12 '16 at 23:29
  • 1
    <%= f.select :native_language, options_for_select(Language.order('language ASC').all, Language.find_by(:language => "English").id) %> – Hasmukh Rathod Aug 12 '16 at 23:31
  • That is getting closer, but now all the dropdown options say 'Language:0x007fa766c07438' instead of the language name. – gwalshington Aug 12 '16 at 23:34
  • Yeah, then replace id with language. I assume language is a column in table. – Hasmukh Rathod Aug 12 '16 at 23:35
  • That has the same result with Language:0x007fa766c07438 – gwalshington Aug 12 '16 at 23:36
  • You are getting whole object of Language. In this , Language.find_by(:language => "English").id, replace id with attribute you want to see in dropdown. I guess attribute is language. So it would be "Language.find_by(:language => "English").language" – Hasmukh Rathod Aug 12 '16 at 23:41
  • I did, and I am getting the same result. Here is my current code: <%= f.select :native_language, options_for_select(Language.order('language ASC').all, Language.find_by(:language => "English").id) %> I feel like the problem is that it is not telling it to show language somewhere else. My original one called it twice – gwalshington Aug 12 '16 at 23:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120842/discussion-between-hasmukh-rathod-and-gwalshington). – Hasmukh Rathod Aug 12 '16 at 23:45