0

Hi I am trying to set ":selected => nil" or to a value from a variable like ":selected => val" in the select() in ruby but it is not working in either ways. Please can anyone tell what is wrong with the given below snippet. Thanks

<%= select( map1[:field_name], "id", map1[:field_codes], :selected => nil )%>

<%= select( map1[:field_name], "id", map1[:field_codes], :selected => val )%>

where map1[:field_name] is a string 
and map1[:field_codes] returns a hash
val is a variable containing one of the values from hash.
4

2 回答 2

0

你的片段工作得很好。用它测试过

hash = { "n" => "no", "y" => "yes" }
val = "yes"
ActionView::Base::new.select("name", "id", hash, selected: val)

它创造了:

<select id="name_id" name="name[id]">
   <option value="no">n</option>
   <option selected="selected" value="yes">y</option>
</select>

您的值valmap1[:field_codes]值不完全匹配,或者如果您启动 Rails 应用程序并检查表单以查看selected="selected",这可能是 Rails 之外的问题(例如您的浏览器)。

于 2013-07-30T16:09:26.027 回答
-1

尝试在选项周围放置花括号:

 {:selected => val}
于 2013-07-30T14:29:17.920 回答