【问题标题】:Association :patient not found, rails 4协会:未找到患者,轨道 4
【发布时间】: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”调用操作“新”。
  • 调用新操作时,您还必须发送paciente id。但是您的视图没有发送此 ID。您能否发布您的视图以查看发送的内容。
  • 请用西班牙语和英语代码检查您的应用程序。对于标准,包括您的英文代码(控制器、模型、视图)。
  • 好的,我编辑帖子以将西班牙语翻译成英语,不便之处敬请见谅。
  • @Eleazar Enrique,我怎样才能将动作称为“新”并将患者“id”传递给动作?感谢您的帮助

标签: ruby-on-rails controller associations


【解决方案1】:

在 Rails 中创建关联时,请特别注意多元化!

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

您会注意到您的 Consultum 类没有 patient 关系。它有belongs_to :patients

class Consultum < ActiveRecord::Base
  belongs_to :patient
  belongs_to :user
  has_one :recetum
end

您的属性命名也应遵循 Ruby 约定!

Ruby 使用snake_case 命名属性和方法,并且会自动将任何以大写字母开头的标识符视为常量!

这不仅仅是一个风格问题 - MRI Ruby 将允许您重新分配带有警告的常量。其他版本或未来版本可能不那么宽松。

Bad                       Good
.Reason_Consultation      .reason_consultation
.Diagnostic               .diagnostic
.TX                       .tx
.Lab                      .lab
.Observations             .observations

https://github.com/bbatsov/ruby-style-guide

【讨论】:

  • 谢谢你,我听从了你的评论,我改变了 Rails 约定的属性。
  • 如果我在 Consultum 课程中理解得很好,那么我必须删除 belongs_to :patient,然后将关系更改为 1 到 many? has_many:病人??这就是为什么我不能将患者和咨询联系起来?谢谢你的时间。
  • 不,咨询应该belong_to :patientguides.rubyonrails.org/association_basics.html
【解决方案2】:

要调用一个动作,你可以传递如下参数:

link_to "New Consultum", new_consultum_path(:id => here, yo have to put the paciente id)

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多