处理多个对象

时间:2015-11-16 03:53:42

标签: ruby-on-rails ruby

我是Rails的新手,目前正在尝试将患者添加到现有的牙医预约中。我无法正确设置我的视图和控制器。我怎样才能正确完成这个?

注意:按照我设置的方式,我可以创建约会并将其与牙医联系起来。当然,patient_id丢失了。

型号:

class Dentist < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :dentists
  belongs_to :patients
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :dentists, :through => :appointments
end

架构:

ActiveRecord::Schema.define(version: 20151107052115) do
  create_table "appointments", force: :cascade do |t|
    t.integer  "dentist_id"
    t.integer  "patient_id"
    t.datetime "appt_date"
  end

  create_table "dentists", force: :cascade do |t|
    t.string "name"
  end

  create_table "patients", force: :cascade do |t|
    t.string "name"
  end
end

路线:

Rails.application.routes.draw do
  concern :commentable do
    resources :appointments
  end

  resources :dentists, concerns: :commentable
  resources :patients, concerns: :commentable
end

牙医控制员:

class DentistsController < ApplicationController
  def new
    @dentist = Dentist.new
  end

  def create
    @dentist = Dentist.new(dentist_params)
    if @dentist.save
      redirect_to dentists_path
    else
      render :new
    end
  end
...
end

约会控制员:

class AppointmentsController < ApplicationController
  def new
    @dentist = Dentist.find(params[:dentist_id])
    @appointment = @dentist.appointments.new
  end

  def create
    @dentist = Dentist.find(params[:dentist_id])
    @appointment = @dentist.appointments.new(appt_params)
    if Appointment.exists?(:appt_date => @appointment.appt_date)
      render :new
    else
      @appointment.save
      redirect_to dentist_path(@dentist)
    end
  end
...
end

患者控制者:

TBD

牙医查看(显示):

<p><%= @dentist.name %> DDS</p>

<% if @dentist.appointments.any? %>
  <% @dentist.appointments.each do |appt| %>
    <ul>
      <li><%= appt.appt_date %></li>
      <p><%= link_to "Edit", edit_dentist_appointment_path(@dentist, appt) %> |
         <%= link_to 'Delete', dentist_appointment_path(@dentist, appt), :method => :delete,
             data: {:confirm => 'Are you sure you want to delete this record?'} %> |
         <%= link_to 'Add Patient', new_patient_path %></p>
    </ul>
  <% end %>
<% else %>
  <p>There are currently no appointments scheduled</p>
<% end %>

<p><%= link_to 'Delete Dentist', dentist_path(@dentist), :method => :delete,
       data: {:confirm => 'Are you sure you want to delete this record?'} %></p>


<p><%= link_to 'Create an appointment', new_dentist_appointment_path(@dentist) %></p>

<p><%= link_to 'Return to list', root_path %></p>

2 个答案:

答案 0 :(得分:0)

我想您所问的是,您是否可以同时通过牙医模型和患者模型创建约会eg. @dentist.@patient.apointment.new 你不能这样做。根据您设置的关系,您可能希望像现在一样从牙医创建appt,并将患者ID作为属性传递,反之亦然。或者,通过约会模型eg. Appointment.new(dentist: @dentist, patient: @patient, ...)

创建

答案 1 :(得分:0)

  

我是Rails的新手

欢迎!

您需要将class Appointment < ActiveRecord::Base belongs_to :dentist belongs_to :patient end 引用更改为单数

appointment#edit

-

因为我无法查看您尝试实现此功能的位置,所以我会告诉您我的行为(使用#app/controllers/appointments_controller.rb class AppointmentsController < ApplicationController def edit @appointment = Appointment.find params[:id] end def update @appointment = Appointment.find params[:id] @appointment.save appointment_params end private def appointment_params params.require(:appointment).permit(:dentist_id, :patient_id, :appt_date) end end #app/views/appointments/edit.html.erb <%= form_for @appointment do |f| %> <%= f.collection_select :patient_id, Patient.all, :id, :name %> <%= f.submit %> <% end %> 操作):

patient

-

如果您尝试使用appointments#create方法设置#app/controllers/appointments_controller.rb class AppointmentsController < ApplicationController def new @dentist = Dentist.find params[:id] @appointment = @dentist.appointments.new end def create @dentist = Dentist.find params[:id] @appointment = @dentist.appointments.new appointment_params end private def appointment_params params.require(:appointment).permit(:dentist_id, :patient_id, :appt_date) end end #app/views/appointments/new.html.erb <%= form_for @appointment do |f| %> <%= f.collection_select :patient_id, Patient.all, :id, :name %> <%= f.submit %> <% end %> ,那么您最好这样做:

function dialogAc(ID1) {
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("/faturaIrsaliye/FaturaKaydet.aspx/abc") %>',
        data: "{ID1:'" + ID1 + "' }",
        dataType: 'json',
        async: true,
        cache: false,
        contentType: 'application/json; charset=utf-8',
        success: function (msg) {
            $("#IrsaliyeDetay").append(msg.d);
        },
        failure: function (msg) {
            alert("Error");
        }
    });
    ID11 = ID1;
    $("#dialog-form").dialog("open");
}