设置 helper_method :current_patient Rails 4

set up helper_method :current_patient Rails 4

我如何在我的应用程序中设置一个辅助方法,当我调用它时,识别来自某个医生(可以是多个医生,一个医生可以是多个患者)的 current_patient 并访问患者: id,这将帮助我联想:

医护人员为患者提供咨询 current_patient

我需要访问患者并在咨询中设置 id(患者)table 外键:patient_id

在控制器中的创建操作中,我有:

def create
  @consultation = Consultation.new(consultation_params.merge({:patient_id => current_patient}))
  respond_to ...
end

辅助方法是执行此操作的好方法吗?

我该怎么做?

我的模特:

class Medic < ActiveRecord::Base
  has_many :patients
end

class Patient < ActiveRecord::Base
  belongs_to :medic, :foreign_key => :medic_id
  has_many :consultations
  accepts_nested_attributes_for :consultations
end

class Consultation < ActiveRecord::Base
  belongs_to :patient, :foreign_key => :patient_id
end

感谢帮助

让我们从路线开始:

resources :patients do
  resources :consultations
end

这将为我们提供嵌套在患者下方的 consultations 的路线:

                   Prefix Verb   URI Pattern                                            Controller#Action
    patient_consultations GET    /patients/:patient_id/consultations(.:format)          consultations#index
                          POST   /patients/:patient_id/consultations(.:format)          consultations#create
 new_patient_consultation GET    /patients/:patient_id/consultations/new(.:format)      consultations#new
edit_patient_consultation GET    /patients/:patient_id/consultations/:id/edit(.:format) consultations#edit
     patient_consultation GET    /patients/:patient_id/consultations/:id(.:format)      consultations#show
                          PATCH  /patients/:patient_id/consultations/:id(.:format)      consultations#update
                          PUT    /patients/:patient_id/consultations/:id(.:format)      consultations#update
                          DELETE /patients/:patient_id/consultations/:id(.:format)      consultations#destroy

所以假设我们有一个显示所有患者的 PatientsController#index 方法。在我们看来,我们有这样的东西:

<% @patients.each do |patient| %>
  <li>
    <p><%= patient.name %></p>
  </li>
<% end %>

所以让我们添加一个 link 来创建咨询:

<% @patients.each do |patient| %>
  <p><%= patient.name %></p>
  <ul class="actions">
    <li>
     <%= link_to "New consultation",
                 new_patient_consultation_path(patient) %>
    </li>
  </ul>
<% end %>

单击 link 会将我们带到 /patients/6/consultations/new

所以在我们的 ConsultationsController 中,我们可以从参数中访问患者 ID:

class ConsultationsController < ApplicationController

  # We use a callback so that we don't need to do 
  # @patient = Patient.find(params[:id]) in every action
  before_action :set_patient 

  # GET /patients/:patient_id/consultations/new
  def new
    @consultation = @patient.consultations.new
  end

  # POST /patients/:patient_id/consultations
  def create
    @consultation = @patient.consultations.new(consultation_params)

    # ...
  end

  # ...

  private
    def set_patient
      @patient = Patient.find(params[:patient_id])
    end
    # ...
end

set_patient只是属于controller的私有方法。这不是真正需要使用辅助方法的情况。

在处理身份验证时经常这样做,因为您从会话中获得 current_user - 独立于参数。如果您正在创建一个辅助方法,它应该可以在任何地方使用。

你还需要做最后一件事才能让它工作,表单需要指向 /patients/:patient_id/consultations

# app/views/consultations/_form.html.erb
<% form_for [@patient, @consultation] do |f| %>
   # .. rails does all the magic figuring out the url.
<% end %>