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

前端之家收集整理的这篇文章主要介绍了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 %>
原文链接:https://www.f2er.com/ruby/273562.html

猜你在找的Ruby相关文章