2015-01-03 34 views
0

我有一个名为Car的模型的Rails表单,其中:location属性的分组选择字段使用grouped_options_for_select表单助手创建。我遇到的问题是,如果要编辑我已保存到数据库的Car,则存储在:location属性 - :location_id中的值不会显示在选择字段中作为选定值。相反,选择字段显示为空白,未选择任何值。我有其他选择字段的表单上没有分组,并没有这个问题。我的表单辅助看起来像Rails分组选择字段不显示选定的值

<%= car_info_field.select :location_id, grouped_options_for_select(@grouped_locations), 
      { include_blank: true }, { class: "form-control" } %> 

我的模型看起来像

class Car < ActiveRecord::Base 
    belongs_to :location 
    validates :location_id, presence: true 

我怎样才能获得分组选择字段显示编辑表单上选择权值?

+0

我想你需要为你的location_id添加attr_accessible – trueinViso

回答

1

grouped_options_for_select帮助程序不知道表单构建器,并且您需要显式地传递“key to select”。

grouped_options_for_select(@grouped_locations, car_info_field.object.location_id) 
相关问题