创建预约时,例如组织“ABC”,我还可以看到属于其他组织的医生。它假设只列出来自“ABC”的医生而不是其他人。我该怎么办。

谢谢你。

我的预约表格:

<%= simple_form_for(@appointment, :html => { :class => 'form-horizontal' }) do |f| %>
  <div class="form-inputs">
    <%= f.hidden_field :patient_id %>
    <%= f.association :physician, :label_method => :first_name, :include_blank => false, :as => :radio_buttons, :required => true %>
    <%= f.hidden_field :appointment_date, :value => DateTime.now  %>
    <%= f.hidden_field :organization_id, :value => current_user.organization_id%>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Create Appointment" %>
  </div>
<% end %>

我的模型:

/app/models/physician.rb
class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
  belongs_to :organization

  attr_accessible :physician_name, :organization_id
end

/app/models/appointment.rb
class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
  belongs_to :organization

  attr_accessible :physician_id, :patient_id, :appointment_date, :state, :organization_id
end

/app/models/patient.rb
class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
  belongs_to :organization

  attr_accessible :patient_name, :organization_id
end

我的 Controller :

/app/controllers/appointment_controller.rb
class AppointmentsController < ApplicationController
  def new
    @appointment = Appointment.new
    @appointment.patient_id = params[:patient_id]
  end

  def create
    @appointment = Appointment.new(params[:appointment])

    if @appointment.save
      flash[:notice] = "New appointment record created"
      redirect_to dashboards_path
    else
      render 'new'
    end
  end
end

最佳答案

这是因为 simple_form 不知道您的范围。如果你告诉它:

<%= f.association :phyisician %>

它将简单地列出数据库中所有可用的医生。

解决方案是给它你想要展示的医生的集合,例如你可以写:
<%= f.association :physician,
                  :collection => @appointment.patient.organization.physicians,
                  :label_method => :first_name,
                  :include_blank => false,
                  :as => :radio_buttons,
                  :required => true %>

关于ruby-on-rails - 如何在表格中过滤或限定医师仅列出属于组织的医师?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11916137/

10-13 02:09