Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails has_many:通过嵌套形式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚通过协会跳入了has_many:我试图通过单一的形式实现对所有3个表(医师,患者和关联表)保存数据的能力.

我的迁移:

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,但被卡住了一些或其他错误实现它.

解决方法

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

楷模:

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,请注明来意。