1

在我的 Rails 应用程序中,我试图为TrainingClass模型设置创建表单。我希望此表单允许用户在同一个表单中创建多个ClassMeeting模型(与模型有 belongs_to 关系TrainingClass),我正在使用accepts_nested_attributes_for它来执行此操作。不幸的是,每当我提交表单时,我都会收到错误消息:Meetings class can't be blank.

我意识到这是因为ClassMeetinghas validates :class_id, presence: true,并且因为TrainingClass在保存之前不能有 id ,但我不确定解决这个问题的正确方法。(我能想到几种可能的方法,但它们并不是完全优雅的解决方案。)有什么想法吗?我很感激你能给我的任何帮助。

注意:我意识到过去已经提出了很多类似的问题。但是,这些问题中的大多数都是旧的并且已经过时的答案,并且没有一个能解决我的问题。


这是我的代码。请记住,尽管为了简洁起见我已经简化了它的某些方面,但模型之间的关系ClassMeetingTrainingClass没有被触及:

课堂会议模式:

# == 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 %>
4

2 回答 2

0

我仔细研究了这个示例,尝试了与我的代码的差异,但最终我的问题通过使用:inverse_of.

请参阅accept_nested_attributes_for 子关联验证失败

于 2013-06-06T04:14:14.890 回答
0

好的,我找到了解决问题的方法。我需要做的就是验证模型中是否存在 training_class 而不是 class_id ClassMeeting。这样训练类的存在仍然得到验证,但验证器不会干扰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
于 2012-08-02T19:22:47.460 回答