问题
I'm having trouble getting the selected value using the select_tag. On my new.html.erb, I have a following code:
<%= select_tag "dams_provenance_collection[subjectType][]", options_for_select(subjectTypeArray), :prompt=>"Select subject type" %>
and on edit.html.erb, I want to use the selected value from new.html.erb as a default, so I tried:
<%= select_tag 'dams_provenance_collection[subjectType][]', options_for_select(subjectTypeArray, params[:selected]) %>
but it didn't work. Does anyone know how to set selected value from new.html.erb as default in edit.html.erb? Any help would be appreciated.
回答1:
Try this,
<%= select_tag "dams_provenance_collection[subjectType][]", options_for_select(array_values, :selected => params[:option]) %>
回答2:
Just try like this:
= select_tag :chart_time_id, options_for_select(@times_collect, :selected => params[:num_days].present? ? params[:num_days].to_i : 7), {class: 'form-control selectpicker', 'data-style'=> "btn-info", required: true}
Just check param present or not, and using inline operater (if -else)
来源:https://stackoverflow.com/questions/17608003/ruby-on-rails-select-tag-selected-value