如何在“options_from_collection_for_select”(Rails 3.2)中设置“默认选项”?

时间:2014-10-13 15:43:16

标签: ruby-on-rails ruby ruby-on-rails-3 select

我试图提供@languages的默认选项 - '所有语言' 以下是观点:

.dropdown-search-filter
   = select_tag "languages", options_from_collection_for_select(@languages, "id", "to_label", selected: params[:languages]), prompt: "All Languages"
.dropdown-search-filter.search-select-tag
   = select_tag "destination", content_tag(:option,'All Destinations',:value=>"all")+options_from_collection_for_select(@destinations, "id", "name", selected: params[:destination])

它仍然将英语作为默认值:here screenshot

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您正试图访问视图中的params,但您无法访问。创建一个像@language_id这样的实例变量,并在视图中使用它

答案 1 :(得分:0)

正如我可以在文档(link)中看到的那样:

select_tag "languages", options_from_collection_for_select(@languages, "id", "to_label", params[:languages]), prompt: "All Languages"

请注意,params[:languages]必须是您的语言集合的有效ID,因为第四个参数(默认选择)基于选项的id(第二个参数中指定的字段)。

这对你有用吗?