rails select_tag选择无效

时间:2015-05-15 19:05:27

标签: ruby-on-rails ruby

我不确定我做错了什么,但我的select_tag :selected无效。检查以下代码:

<%= select_tag :supplier, options_from_collection_for_select(SupplierItem.select("DISTINCT(SUPPLIER)").group("SUPPLIER"), "SUPPLIER", "SUPPLIER"), { :selected => params[:supplier], prompt: "Select a Category"} %>

2 个答案:

答案 0 :(得分:2)

我让:selected => params[:supplier]在错误的地方。下面的工作代码:

<%= select_tag :supplier, options_from_collection_for_select(SupplierItem.select("DISTINCT(SUPPLIER)").group("SUPPLIER"), "SUPPLIER", "SUPPLIER" ,:selected => params[:supplier]), {prompt: "Select a Category"}  %>

答案 1 :(得分:0)

根据the docsselect_tag不接受:selected选项,因为<option>标记在options_from_collection_for_select之前已全部由select_tag呈现{ {1}}被调用。

相反,根据the docsoptions_from_collection_for_select采用选定的值作为可选的第四个参数。