【发布时间】:2015-11-11 19:44:12
【问题描述】:
我正在尝试将 1 位患者之间的关系与咨询相关联,但出现错误:
关联:未找到患者
在我的模型中:
class Patient < ActiveRecord::Base
belongs_to :user
has_many :consultums, through: :patients
end
class Consultum < ActiveRecord::Base
belongs_to :patients
belongs_to :user
has_one :recetum
end
在我拥有的控制器中:
class ConsultationController < ApplicationController
before_action :authenticate_user!
before_action :set_consultum, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@consultation = Consultum.all
respond_with(@consultation)
end
def show
respond_with(@consultum)
end
## when I try to create a new consultation, throw me the error ##
def new
@consultum = Consultum.new
# patient_id
respond_with(@consultum)
end
def edit
end
def create
@consultum = Consultum.new(consultum_params)
@consultum.save
respond_with(@consultum)
end
def update
@consultum.update(consultum_params)
respond_with(@consultum)
end
def destroy
@consultum.destroy
respond_with(@consultum)
end
# def patient_id
# patient = Patient.find(params[:id])
# # patient = @patient.id
# @consultum.patient_id = patient
# end
private
def set_consultum
@consultum = Consultum.find(params[:id])
end
def consultum_params
params.require(:consultum).permit(:Reason_Consultation, :Diagnostic, :TX, :Labs, :Observations, :patient_id)
end
end
所以,如您所见,我创建了函数 patient_id,并尝试从“患者”中检索 id 并将其放入“咨询”表中的患者 ID,但似乎不起作用...
如果我取消对 patient_id 函数的注释,则会向我抛出此错误:
Couldn't find Patient without an ID
我被卡住了,你知道吗?
提前致谢
编辑
在我看来,我有:
咨询/new.html.erb
<div class="page-header">
<%= link_to consultation_path, class: 'btn btn-default' do %>
<span class="glyphicon glyphicon-list-alt"></span>
Back
<% end %>
<h1>New consultum</h1>
</div>
<%= render 'form' %>
_form.html.erb
<%= simple_form_for(@consultum) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :Reason_Consultation %>
<%= f.input :Diagnostic %>
<%= f.input :TX, placeholder: "optional" %>
<%= f.input :Labs, placeholder: "optional" %>
<%= f.input :Observations, placeholder: "optional" %>
<%= f.association :patient %>
</div>
<div class="form-actions">
<%= f.button :submit, "Save Consultation" %>
</div>
<% end %>
【问题讨论】:
-
动作'new'没有收到参数ID,这就是原因。您必须通过患者“ID”调用操作“新”。
-
调用新操作时,您还必须发送
pacienteid。但是您的视图没有发送此 ID。您能否发布您的视图以查看发送的内容。 -
请用西班牙语和英语代码检查您的应用程序。对于标准,包括您的英文代码(控制器、模型、视图)。
-
好的,我编辑帖子以将西班牙语翻译成英语,不便之处敬请见谅。
-
@Eleazar Enrique,我怎样才能将动作称为“新”并将患者“id”传递给动作?感谢您的帮助
标签: ruby-on-rails controller associations