在Rails中,如何使用accepts_nested_attributes_for创建嵌套对象?
在我的Rails应用程序中,我正在尝试为TrainingClass
模型设置创建表单。 我希望此表单允许用户在同一表单中创建多个ClassMeeting
模型(与TrainingClass
模型具有belongs_to关系),并且我使用accepts_nested_attributes_for
来执行此操作。 不幸的是,每当我提交表单时,我都会收到错误消息: Meetings class can't be blank
。
我意识到这是因为ClassMeeting
已经validates :class_id, presence: true
,并且因为TrainingClass
在保存之前不能拥有id,但是我不确定是否有正确的方法来解决这个问题。 (我可以想到几种可能的方法,但它们并不是完全优雅的解决方案。)任何想法? 我很感激你能给我的任何帮助。
注意:我发现过去有很多类似的问题。 但是,大多数问题都是陈旧的,并且已经过时了,而且没有一个能解决我的问题。
这是我的代码。 请记住,虽然为简洁起见,我简化了它的某些方面,但ClassMeeting
和TrainingClass
模型之间的关系保持不变:
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
TrainingClass模型:
# == 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
TrainingClass表格(查看):
Schedule
好吧,我找到了解决问题的方法。 我需要做的就是在ClassMeeting
模型中validationtraining_class而不是class_id的存在。 这样,训练类的存在仍然有效,但validation器不会干扰accepts_nested_attributes_for
的保存模型的方法:
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
我仔细研究了这个例子,尝试了与我的代码的差异,但最后我的问题通过使用:inverse_of
。
请参阅accepts_nested_attributes_for子关联validation失败
- Eventmachine start_tcp_server没有接受者 – 端口正在使用或需要root权限(RuntimeError)
- 从回形针获取url(RoR)
- 如何在squeel中动态处理where条件?
- rails – 实现一个简单的锁,以防止用户同时编辑相同的数据
- 如何在spec_helper.rb中为Capybara Webkit设置ignore_ssl_errors选项
- 如何在测试我的应用程序之前自动加载测试数据库中的数据?
- ActiveRecord查询比直接SQL慢得多?
- 接收错误PG :: UndefinedColumn:ERROR:使用Geocoder的near方法时,mymodels.distance列不存在
- Rails:仅当另一个字段存在时才validation字段