【发布时间】:2016-09-25 15:20:17
【问题描述】:
我有一个Profile 模型,即accepts_nested_attributes_for :grades。
我的Grade 模型如下所示:
# == Schema Information
#
# Table name: grades
#
# id :integer not null, primary key
# subject :string
# result :string
# grade_type :integer
# profile_id :integer
# created_at :datetime not null
# updated_at :datetime not null
class Grade < ActiveRecord::Base
belongs_to :profile
enum grade_type: { csec: 0, cape: 1, sat: 2, g7: 3, g8: 4, g9: 5, g10: 6, g11: 7, g12: 8, g13: 9 }
end
在我的profiles/_form.html.erb 中,我有以下内容:
<%= simple_form_for @profile, html: { class: "form-horizontal" } do |f| %>
<%= f.simple_fields_for :grades, html: { class: "form-inline" } do |g| %>
<%= g.input_field :grade_type, collection: Grade.grade_types.keys, include_blank: false, class: 'col-lg-8 form-control' %>
<% end %>
<% end %>
所以,这向我显示了枚举值中的grade_types,就像我期望的那样。
但是我想要发生的是,当我正在编辑一条记录并显示成绩时,它应该预先选择了grade_type。
我该怎么做?
【问题讨论】:
-
您是否将控制器中的
grade_types参数列入白名单?require(:profile).permit(grades_attributes: [:grade_type]) -
@max 是的。那不是问题。问题在于检测已选择的内容并对其进行识别。
-
嗯,试试
collection: User.statuses.map {|k,v| [k, k] }- SimpleForm 将ActionView::Helpers::FormOptionsHelper委派下来,将g.object.grade_type链接到选项时可能会遇到问题。
标签: ruby-on-rails simple-form nested-forms nested-attributes