在代码中我有:
<%= 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' %>
我正在使用此表单来更新“cuisine_name”。问题是我想为此字段设置默认值。现在我正在获取所有可用值。
最佳答案
你就快到了。来自docs :
options_from_collection_for_select(collection, value_method, text_method, selected = nil)
因此您需要再传递一个参数来创建默认选择的选项
options_from_collection_for_select(@cuisines,"id","cuisine_name", <your thing here>)
关于ruby-on-rails - 如何为表单内的选择字段设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27169705/