2013-07-12 73 views
-1

我无法使用select_tag获取选定的值。 在我new.html.erb,我有以下代码:Ruby on Rails select_tag选定值

<%= 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]) %> 

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

回答

2

试试这个,

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

我认为这是错误的,你应该通过'selected'选择'select_tag'不是选项 –

+0

或者如果你想这样做,只需将选择的(默认)作为第二个参数。 –

+0

@MichaelSzyndel:OKie ..让我检查一下,然后编辑..谢谢.. – Jyothu

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存在与否,以及使用内联算子(如果-else)