简体   繁体   English

更新一个模型并在一种表单中创建其他模型Rails 3.2.5

[英]Update one model and create other model in one form Rails 3.2.5

I have one form that can create two models perfectly, but, is there any way to update one of the models and create the other? 我有一个表格可以完美地创建两个模型,但是,有什么方法可以更新其中一个模型并创建另一个模型吗? Resuming: I have a form for creating an appointment and also I can create the person for this appointment. 继续:我有一个用于创建约会的表格,也可以为该约会创建人。

def new
  @Cita = Cita.new
  @Cita.build_paciente

  render :new
end

def create

 @Cita = Cita.new(params[:cita])

 if @Cita.save
  redirect_to :action => 'hoy'
 else
  render  'new'

end 结束

This is working pretty well when I create the two models at time, but if I fill the person fields ... How can I do for UPDATING the person attributes ("paciente") and creating the appointment ("Cita") for that person. 当我同时创建两个模型时,这工作得很好,但是如果我填写人员字段...我该如何更新人员属性(“ paciente”)并为该人员创建约会(“ Cita”) 。

Thanks. 谢谢。

Finally I solved it adding a hidden field filling the person_ID on the person form. 最后,我解决了此问题,并在人员表单上添加了一个隐藏字段来填充person_ID。 When you send the ID in the model attributes rails makes an update instead of an insert for that model while creates the other model associating the nested model. 当您在模型属性中发送ID时,rails会对该模型进行更新,而不是对该模型进行插入,同时会创建另一个与嵌套模型相关联的模型。 If this hidden field is empty, rails create the two models at time. 如果此隐藏字段为空,则rails会同时创建两个模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM