大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails has_many:通过嵌套形式,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的迁移:
class CreatePhysicians < ActiveRecord::Migration def self.up create_table :physicians do |t| t.String :name t.timestamps end end end class CreatePatients < ActiveRecord::Migration def self.up create_table :patients do |t| t.String :name t.timestamps end end end class CreateAppointments < ActiveRecord::Migration def self.up create_table :appointments do |t| t.Integer :physician_id t.Integer :patient_id t.date :appointment_date t.timestamps end end end
我的型号:
class Patient < ActiveRecord::Base has_many :appointments has_many :physicians,:through => :appointments accepts_nested_attributes_for :appointments accepts_nested_attributes_for :physicians end class Physician < ActiveRecord::Base has_many :appointments has_many :patients,:through => :appointments accepts_nested_attributes_for :patients accepts_nested_attributes_for :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end
我的控制器:
def new @patient = Patient.new @patient.physicians.build @patient.appointments.build end
我的观点(new.html.rb):
<% form_for(@patient) do |patient_form| %> <%= patient_form.error_messages %> <p> <%= patient_form.label :name,"Patient Name" %> <%= patient_form.text_field :name %> </p> <% patient_form.fields_for :physicians do |physician_form| %> <p> <%= physician_form.label :name,"Physician Name" %> <%= physician_form.text_field :name %> </p> <% end %> <p> <%= patient_form.submit 'Create' %> </p> <% end %> <%= link_to 'BACk',patients_path %>
我可以创建一个新的患者,医师和相关记录的预约,但是现在我想要在表单中设置约会日期的字段.我应该在哪里放置预约的字段以及我的控制器需要做哪些更改?我尝试了谷歌搜索,尝试了this,但被卡住了一些或其他错误实现它.
楷模:
class Patient < ActiveRecord::Base has_many :appointments has_many :physicians,:through: :appointments accepts_nested_attributes_for :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient accepts_nested_attributes_for :physician end class Physicians < ActiveRecord::Base has_many :appointments has_many :patients,through: :appointments end
控制器:
def new @patient= Patient.new @appointments = @patient.appointments.build @physician = @appointments.build_physician end def create Patient.new(patient_params) end def patient_params params.require(:patient).permit(:id,appointments_attributes: [:id,:appointment_time,physician_attributes: [:id ] ) end
视图
<% form_for(@patient) do |patient_form| %> <%= patient_form.error_messages %> <p> <%= patient_form.label :name,"Patient Name" %> <%= patient_form.text_field :name %> </p> <% patient_form.fields_for :appointments do |appointment_form| %> <p> <%= appointment_form.label :appointment_date,"Appointment Date" %> <%= appointment_form.date_field :appointment_date %> </p> <% appointment_form.fields_for :physician do |physician_form| %> <p> <%= physician_form.label :name,"Physician Name" %> <%= physician_form.text_field :name %> </p> <% end %> <% end %> <p> <%= patient_form.submit 'Create' %> </p> <% end %> <%= link_to 'BACk',patients_path %>
以上是大佬教程为你收集整理的ruby-on-rails – Rails has_many:通过嵌套形式全部内容,希望文章能够帮你解决ruby-on-rails – Rails has_many:通过嵌套形式所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。