ruby-on-rails – Rails has_many:通过嵌套形式

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails has_many:通过嵌套形式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚通过协会跳入了has_many:我试图通过单一的形式实现对所有3个表(医师,患者和关联表)保存数据的能力.

我的迁移:

  1. class CreatePhysicians < ActiveRecord::Migration
  2. def self.up
  3. create_table :physicians do |t|
  4. t.string :name
  5. t.timestamps
  6. end
  7. end
  8. end
  9.  
  10. class CreatePatients < ActiveRecord::Migration
  11. def self.up
  12. create_table :patients do |t|
  13. t.string :name
  14. t.timestamps
  15. end
  16. end
  17. end
  18.  
  19. class CreateAppointments < ActiveRecord::Migration
  20. def self.up
  21. create_table :appointments do |t|
  22. t.integer :physician_id
  23. t.integer :patient_id
  24. t.date :appointment_date
  25. t.timestamps
  26. end
  27. end
  28. end

我的型号:

  1. class Patient < ActiveRecord::Base
  2. has_many :appointments
  3. has_many :physicians,:through => :appointments
  4. accepts_nested_attributes_for :appointments
  5. accepts_nested_attributes_for :physicians
  6. end
  7. class Physician < ActiveRecord::Base
  8. has_many :appointments
  9. has_many :patients,:through => :appointments
  10. accepts_nested_attributes_for :patients
  11. accepts_nested_attributes_for :appointments
  12. end
  13. class Appointment < ActiveRecord::Base
  14. belongs_to :physician
  15. belongs_to :patient
  16. end

我的控制器:

  1. def new
  2. @patient = Patient.new
  3. @patient.physicians.build
  4. @patient.appointments.build
  5. end

我的观点(new.html.rb):

  1. <% form_for(@patient) do |patient_form| %>
  2. <%= patient_form.error_messages %>
  3. <p>
  4. <%= patient_form.label :name,"Patient Name" %>
  5. <%= patient_form.text_field :name %>
  6. </p>
  7. <% patient_form.fields_for :physicians do |physician_form| %>
  8. <p>
  9. <%= physician_form.label :name,"Physician Name" %>
  10. <%= physician_form.text_field :name %>
  11. </p>
  12. <% end %>
  13.  
  14. <p>
  15. <%= patient_form.submit 'Create' %>
  16. </p>
  17. <% end %>
  18.  
  19. <%= link_to 'Back',patients_path %>

我可以创建一个新的患者,医师和相关记录的预约,但是现在我想要在表单中设置约会日期的字段.我应该在哪里放置预约的字段以及我的控制器需要做哪些更改?我尝试了谷歌搜索,尝试了this,但被卡住了一些或其他错误实现它.

解决方法

好的,这个问题的这个小怪物让我呆了几个小时,所以我要在这里发布我的工作解决方案,希望它能够窥视一段时间.这是Rails 4.0和Ruby 2.0.这也克服了我所拥有的“符号到整数转换”问题.

楷模:

  1. class Patient < ActiveRecord::Base
  2. has_many :appointments
  3. has_many :physicians,:through: :appointments
  4. accepts_nested_attributes_for :appointments
  5. end
  6.  
  7. class Appointment < ActiveRecord::Base
  8. belongs_to :physician
  9. belongs_to :patient
  10. accepts_nested_attributes_for :physician
  11. end
  12.  
  13. class Physicians < ActiveRecord::Base
  14. has_many :appointments
  15. has_many :patients,through: :appointments
  16. end

控制器:

  1. def new
  2. @patient= Patient.new
  3. @appointments = @patient.appointments.build
  4. @physician = @appointments.build_physician
  5. end
  6.  
  7. def create
  8. Patient.new(patient_params)
  9. end
  10.  
  11.  
  12. def patient_params
  13. params.require(:patient).permit(:id,appointments_attributes: [:id,:appointment_time,physician_attributes: [:id ] )
  14. end

视图

  1. <% form_for(@patient) do |patient_form| %>
  2. <%= patient_form.error_messages %>
  3. <p>
  4. <%= patient_form.label :name,"Patient Name" %>
  5. <%= patient_form.text_field :name %>
  6. </p>
  7.  
  8. <% patient_form.fields_for :appointments do |appointment_form| %>
  9. <p>
  10. <%= appointment_form.label :appointment_date,"Appointment Date" %>
  11. <%= appointment_form.date_field :appointment_date %>
  12. </p>
  13.  
  14. <% appointment_form.fields_for :physician do |physician_form| %>
  15. <p>
  16. <%= physician_form.label :name,"Physician Name" %>
  17. <%= physician_form.text_field :name %>
  18. </p>
  19. <% end %>
  20. <% end %>
  21.  
  22. <p>
  23. <%= patient_form.submit 'Create' %>
  24. </p>
  25. <% end %>
  26.  
  27. <%= link_to 'Back',patients_path %>

猜你在找的Ruby相关文章