Ruby on Rails select_tag selected value

这一生的挚爱 提交于 2020-01-07 08:47:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!