Ruby On Rails - 未定义的方法错误



我正在尝试创建患者的位置,然后创建一个chief_complaint(使用不同的MVC(,以便当我查看患者表索引时,我会看到他们的主要抱怨。我已经在患者表中做了一个complaint_id作为外键,如模式文件所示。但是,我收到以下错误。谁能帮我?

错误消息:nil:NilClass 的未定义方法"名称", NoMethodError in Patients#index

//index.html.erb
    <% @patients.each do |patient| %>
      <tr>
        <td><%= patient.name %></td>
        <td><%= patient.age %></td>
        <td><%= patient.dob %></td>
        <td><%= patient.chief_complaint.name %></td>
        <td><%= link_to 'Show', patient %></td>
        <td><%= link_to 'Edit', edit_patient_path(patient) %></td>
        <td><%= link_to 'Destroy', patient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
//patient form partial
  <div class="field">
    <%= form.label :complaint_id %>
    <%= form.collection_select :complaint_id, @chief_complaints, :id, :name, id: :patient_complaint_id %>
  </div>
// patients show file
<p>
    <strong>Complaint ID:</strong>
    <%= @patient.complaint_id %>
</p>
//chief complaints model
class ChiefComplaint < ApplicationRecord
    has_many :patients
end
//patient model
class Patient < ApplicationRecord
    belongs_to :chief_complaint
end
//schema file
create_table "chief_complaints", force: :cascade do |t|
  t.string "name"
  t.integer "pain"
  t.string "medication"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "patient_id"
end
create_table "patients", force: :cascade do |t|
  t.string "name"
  t.integer "age"
  t.datetime "dob"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "complaint_id"
end
//complete index controller
class PatientsController < ApplicationController
before_action :set_patient, only: [:show, :edit, :update, :destroy]
def index
  @patients = Patient.all
end
def show
end
def new
  @patient = Patient.new
  @chief_complaints = ChiefComplaint.all
end
def edit
  @chief_complaints = ChiefComplaint.all
end
def create
  @patient = Patient.new(patient_params)
respond_to do |format|
  if @patient.save
    format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
    format.json { render :show, status: :created, location: @patient }
  else
    format.html { render :new }
    format.json { render json: @patient.errors, status: :unprocessable_entity }
  end
end
end
def update
  respond_to do |format|
    if @patient.update(patient_params)
      format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
      format.json { render :show, status: :ok, location: @patient }
    else
      format.html { render :edit }
      format.json { render json: @patient.errors, status: :unprocessable_entity }
    end
  end
end
def destroy
  @patient.destroy
  respond_to do |format|
    format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
    format.json { head :no_content }
  end
end
private
  def set_patient
  @patient = Patient.find(params[:id])
end
  def patient_params
    params.require(:patient).permit(:name, :age, :dob, :complaint_id)
  end
  end

尝试这个简短的方法而不使用 if

<%= patient.chief_complaint.try(:name) %>

你可以在这里检查试用的使用情况

它显示错误消息:nil:NilClass 的未定义方法"name",NoMethodError in Patients#index 由于可能是数据将为空白。因此,您可以添加以下条件。

条件

<% if patient.name.present? %>
   <%= patient.name %>
<% end %>

以下行包含错误:

<%= patient.chief_complaint.name %>

您的一位患者chief_complaint没有正确关联。因此,当您查询patient.chief_complaint时,它会返回nil 。并且 nil 类没有name属性。

您可以检查患者是否与chief_complaint有关。喜欢:

<% if patient.chief_complaint.present? %>
 <%= patient.chief_complaint.name %>
<% end %>

最新更新