2 ruby-on-rails ruby-on-rails-4
在代码我有:
<%= form_for(:category,:url=>{:action=>'update',:id=>@category.id}) do |f|%>
<div class="field">
<b style="color: #808080;"> Cuisine name: </b>
<%= f.select :cuisine_id,options_from_collection_for_select(@cuisines,"id","cuisine_name"),prompt:'select cuisine' %>
Run Code Online (Sandbox Code Playgroud)
我正在使用此表单来更新"cuisine_name".问题是我想为这个字段设置默认值.现在我得到所有值.
你快到了.来自文档:
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
Run Code Online (Sandbox Code Playgroud)
所以你需要传递一个参数来创建一个默认选中的选项
options_from_collection_for_select(@cuisines,"id","cuisine_name", <your thing here>)
Run Code Online (Sandbox Code Playgroud)