Search code examples
ruby-on-railsdrop-down-menuform-helpers

How to search in select box options and delete duplicated options using Rails?


I have such task: when user edits his object - first option in the list will be the value of object.

I'm generating my select list using this code:

    <%= f.select :category, options_for_select([
      "#{@website.category}",
      "Banking",
      "Computers",
      "Coupons",
      "Directory",
      "Fashion",
      "Finance",
      "Gifts",
      "Maps",
      "Media",
      "Mobile",
      "News",
      "Tickets",
      "Tech blog",
      "Tech website",
      "Trains",
      "Travel",
      "Transportation - general",
       "Recipes",
      "Another... "])%>

Can anyone suggest how to do it ?


Solution

  • See here the doc about options_for_select

    You can set a default value (or selected value) of the select-box as the second argument. In your case it would be like :

    <%= f.select :category, options_for_select([
      "Banking",
      "Computers",
      "Coupons",
      "Directory",
      "Fashion",
      "Finance",
      "Gifts",
      "Maps",
      "Media",
      "Mobile",
      "News",
      "Tickets",
      "Tech blog",
      "Tech website",
      "Trains",
      "Travel",
      "Transportation - general",
       "Recipes",
      "Another... "], @website.category)%>
    

    Hope this helps!