2012-08-02 68 views
1

在我的Rails应用程序中,我试图为TrainingClass模型设置创建表单。我希望此表单允许用户在同一表格中创建多个ClassMeeting模型(与TrainingClass模型具有belongs_to关系),并且我使用accepts_nested_attributes_for来执行此操作。不幸的是,无论何时提交表单,我都会收到错误消息:Meetings class can't be blank在Rails中,如何使用accept_nested_attributes_for创建嵌套对象?

我意识到,这是因为ClassMeetingvalidates :class_id, presence: true,因为TrainingClass不能有一个ID,直到它被保存后,但我不知道以正确的方式来解决这个问题。 (我可以想到一些可能的方法,但它们并不完全是优雅的解决方案。)任何想法?我会很感激你能给我的任何帮助。

注意:我意识到过去有人问过类似这样的几个问题。然而,这些问题中的大部分都是陈旧的,并且已经过时了,他们都没有解决我的问题。


这是我的代码。请记住,虽然我已经简化为简洁它的某些方面,ClassMeetingTrainingClass模型之间的关系保持不变:

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 

训练类控制器:

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表(视图):

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

回答

0

好吧,我找到了解决我的问题。我需要做的就是验证ClassMeeting模型中是否存在training_class而不是class_id。这样一个培训班的存在仍然是有效的,但验证不保存模型的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 
相关问题