1

I have an f.select in the new article page, and it works fine. However, when I edit the article, the value changes back to whatever is first in the options.

Is there a way to keep the select value so users don't have to worry about redoing that when editing their article?

Here is my select tag:

<%= f.select :category, 
    options_for_select(['drink','food','medicine','supplement','drug','ingredient','lifestyle','other'], params[:category]), {}, { :class => 'span3 controls controls-row' } %>
potashin
  • 44,205
  • 11
  • 83
  • 107
Kathan
  • 1,428
  • 2
  • 15
  • 31

2 Answers2

3

Change:

<%= f.select :category,   options_for_select(['drink','food','medicine','supplement','drug','ingredient','lifestyle','other'], params[:category]), {}, { :class => 'span3 controls controls-row' } %>

to:

<%= f.select :category, options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.category), {}, { :class => 'span3 controls controls-row' } %>

which is similar to what is here: https://stackoverflow.com/a/19120874/3507417

Community
  • 1
  • 1
atw
  • 5,428
  • 10
  • 39
  • 63
1

Instead of params[:category] you should provide selected: :category in the options_for_select(<...>).

potashin
  • 44,205
  • 11
  • 83
  • 107