gpt4 book ai didi

ruby-on-rails - 如何在 simple_form 中处理嵌套关联中的选定值?

转载 作者:行者123 更新时间:2023-12-03 22:40:37 27 4
gpt4 key购买 nike

我有一个 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_type g.object并通过 selected 将其传递到输入字段:

<%= f.input_field :grade_type, collection: Grade.grade_types.keys, selected: g.object.grade_type, include_blank: false, class: 'col-lg-8 form-control' %>

关于ruby-on-rails - 如何在 simple_form 中处理嵌套关联中的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39688509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com