在 Rails 中,如何使用 accepts_nested_attributes_for 创建嵌套对象? [英] In Rails, how do I create nested objects using accepts_nested_attributes_for?
问题描述
在我的 Rails 应用程序中,我试图为 TrainingClass
模型设置创建表单.我希望这个表单允许用户在同一个表单中创建多个 ClassMeeting
模型(它们与 TrainingClass
模型具有belongs_to 关系),我正在使用 接受_nested_attributes_for
来做到这一点.不幸的是,每当我提交表单时,我都会收到错误消息:Meetings class can't be blank
.
In my Rails application, I am attempting to set up creation form for a TrainingClass
model. I want this form to allow the user to create multiple ClassMeeting
models (which have a belongs_to relationship with the TrainingClass
model) within the same form, and I am using accepts_nested_attributes_for
to do this. Unfortunately, whenever I submit the form I get the error message: Meetings class can't be blank
.
我意识到这是因为 ClassMeeting
有 validates :class_id,presence: true
,并且因为 TrainingClass
直到保存后,但我不确定解决此问题的正确方法.(我可以想到几种可能的方法,但它们并不是完全优雅的解决方案.)有什么想法吗?如果您能给我任何帮助,我将不胜感激.
I realize that this is because ClassMeeting
has validates :class_id, presence: true
, and because TrainingClass
can't have an id until after it is saved, but I'm not sure of the right way to get around this. (I can think of a few possible ways, but they aren't exactly elegant solutions.) Any ideas? I'd appreciate any help you can give me.
注意:我意识到过去有人问过很多类似的问题.然而,这些问题中的大多数都是陈旧的,而且答案已经过时,而且没有一个能解决我的问题.
这是我的代码.请记住,虽然为了简洁起见我已经简化了它的某些方面,但是 ClassMeeting
和 TrainingClass
模型之间的关系没有受到影响:
Here is my code. Keep in mind that although I have simplified some aspects of it for brevity, the relationship between the ClassMeeting
and TrainingClass
models was left untouched:
ClassMeeting 模型:
# == Schema Information
#
# Table name: class_meetings
#
# id :integer not null, primary key
# class_id :integer
# start :datetime
# end :datetime
# location :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class ClassMeeting < ActiveRecord::Base
attr_accessible :start, :end, :location
validates :class_id, presence: true
validates :start, presence: true
validates :end, presence: true
validates :location, presence: true, length: {maximum: 255}
belongs_to :training_class, foreign_key: :class_id, inverse_of: :meetings
end
培训班模型:
# == Schema Information
#
# Table name: training_classes
#
# id :integer not null, primary key
# description :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class TrainingClass < ActiveRecord::Base
attr_accessible :description, :meetings_attributes
validates :description, length: {maximum: 255}
has_many :meetings, class_name: :ClassMeeting, foreign_key: :class_id, inverse_of: :training_class
accepts_nested_attributes_for :meetings, allow_destroy: true
end
TrainingClasses 控制器:
class TrainingClassesController < ApplicationController
def new
@training_class = TrainingClass.new()
@training_class.meetings.build
end
def create
@training_class = TrainingClass.new()
if @training_class.update_attributes(params[:training_class])
redirect_to @training_class, notice: 'Class was successfully created.'
else
render "new"
end
end
end
培训班表格(查看):
<%= form_for @training_class do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :description %>
<h2>Schedule</h2>
<%= f.fields_for :meetings do |meeting| %>
<%= meeting.label :start, "Start of Meeting:" %>
<%= meeting.text_field :start %>
<%= meeting.label :end, "End of Meeting:" %>
<%= meeting.text_field :end %>
<%= meeting.label :location, "Location:" %>
<%= meeting.text_field :location %>
<% end %>
<%= f.submit class:"btn btn-large btn-primary" %>
<% end %>
推荐答案
好的,我找到了问题的解决方案.我需要做的就是验证 ClassMeeting
模型中是否存在 training_class 而不是 class_id.这样,训练类的存在仍然被验证,但验证器不会干扰 accepts_nested_attributes_for
保存模型的方法:
All right, I found the solution to my problem. All I need to do is validate the presence of training_class instead of class_id in the ClassMeeting
model. That way the existence of a training class is still validated, but the validator doesn't interfere with accepts_nested_attributes_for
's method of saving the model:
class ClassMeeting < ActiveRecord::Base
attr_accessible :start, :end, :location
validates :training_class, presence: true # :training_class instead of :class_id
validates :start, presence: true
validates :end, presence: true
validates :location, presence: true, length: {maximum: 255}
belongs_to :training_class, foreign_key: :class_id, inverse_of: :meetings
end
这篇关于在 Rails 中,如何使用 accepts_nested_attributes_for 创建嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!