无法获取相关型号的ID /

时间:2018-03-28 14:01:51

标签: ruby-on-rails

我需要在patient控制器中获取reports的ID。每个user都有patients,而每个patient都可以由医生创建reports

我收到此错误:

  

ReportsController #new中的ActiveRecord :: RecordNotFound   找不到'id'=的患者   提取的来源(第49行):

Patient Show Page,链接到新报告:

<% if signed_in? %>
  <% if @patient.id == current_user.id %>
    <%= link_to "Add Visit Report", new_report_path(@patient),class: "button_one" %>
  <% end %>
<% end %>

报告控制器:

def new
  @report = Report.new 
end

private

def report_params
  params.require(:report).permit(:date, :report)
end

def find_patient
  @patient = Patient.find(params[:patient_id])  //Line 49 where error is
end

def find_report
  @report = Report.find(params[:id])
end

用户,患者和报告模型:

class Patient < ActiveRecord::Base

  include Tire::Model::Search
  include Tire::Model::Callbacks

  validates :name, presence:true
  validates :age, presence:true
  validates :phone, presence:true
  validates :ailment, presence:true
  validates :apointment, presence:true
  validates :status, presence:true

  belongs_to :user
  has_many :reports, dependent: :destroy

  def to_s
    name
  end
end

class Report < ActiveRecord::Base
  belongs_to :user
  belongs_to :patient
end

class User < ActiveRecord::Base
  include Clearance::User

  has_many :patients
  has_many :reports, dependent: :destroy
end

路线:

       reports GET    /reports(.:format)                      reports#index
               POST   /reports(.:format)                      reports#create
    new_report GET    /reports/new(.:format)                  reports#new
   edit_report GET    /reports/:id/edit(.:format)             reports#edit
        report GET    /reports/:id(.:format)                  reports#show
               PATCH  /reports/:id(.:format)                  reports#update
               PUT    /reports/:id(.:format)                  reports#update
               DELETE /reports/:id(.:format)                  reports#destroy

1 个答案:

答案 0 :(得分:0)

更新您的链接:

<%= link_to "Add Visit Report", new_report_path(patient_id: @patient.id),class: "button_one" %>

这会将参数patient_id传递给您的控制器中使用的new_report_path@patient = Patient.find(params[:patient_id])