Lino
Lino

Reputation: 229

Ruby on Rails select_tag selected value

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.

Upvotes: 0

Views: 7201

Answers (2)

ThienSuBS
ThienSuBS

Reputation: 1622

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)

Upvotes: 1

Jyothu
Jyothu

Reputation: 3154

Try this,

<%= select_tag "dams_provenance_collection[subjectType][]", options_for_select(array_values, :selected => params[:option]) %>

Upvotes: 3

Related Questions