Ruby on Rails select_tag选择值

时间:2013-07-12 05:32:41

标签: ruby-on-rails ruby selectedvalue

我在使用select_tag获取所选值时遇到问题。 在我的new.html.erb上,我有以下代码:

<%= select_tag "dams_provenance_collection[subjectType][]",  options_for_select(subjectTypeArray), :prompt=>"Select subject type" %>

并且在edit.html.erb上,我想使用new.html.erb中的选定值作为默认值,所以我尝试了:

<%= select_tag 'dams_provenance_collection[subjectType][]',  options_for_select(subjectTypeArray, params[:selected])  %>

但它不起作用。有没有人知道如何在new.html.erb中将选定值设置为edit.html.erb中的默认值?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

试试这个,

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

答案 1 :(得分:1)

试试这样:

= 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}

只检查param是否存在,并使用内联operater(if -else)