【发布时间】:2014-06-24 23:18:27
【问题描述】:
我问过之前的question,然后这个...
我有 3 个模型:合同、附录和预约
Contract.rb
has_many :addendums
accepts_nested_attributes_for :addendums
Addendum.rb
belongs_to :contract
has_many :appointments
Appointment.rb
belongs_to :addendum
创建附录时,会自动创建一些约会。我做了这项工作,像这样在Addendum's Controller 上添加了一个函数
def createNewAppointments
appointment = Appointment.new(params[:appointment]);
appointment.dataprovavel = @addendum.data
appointment.addendum_id = @addendum.id
appointment.appointment_state = AppointmentState.where(descricao:'Pendente').first
appointment.save
...
end
这里调用了这个函数
Addendum Controller
def create
respond_to do |format|
if @addendum.save
format.html { redirect_to @addendum, notice: 'Addendum was successfully created.' }
format.json { render action: 'show', status: :created, location: @addendum }
createNewAppointments
else
...
end
现在,因为我开始在 Contract 和 Addendum 之间使用nested_attributes,所以新的 Addendum 是使用 build 方法创建的,所以函数 createNewAppointments 没有被调用...我该怎么做?
我尝试在模型Addendum 上使用after_create createNewAppointments,但它不起作用。它给出了一个错误Uninitialized constant Contract::Addendum
【问题讨论】:
-
这个
Addendum Controller表示class AddendumsController?
标签: ruby-on-rails-4 nested-attributes