我正在尝试将其创建到我创建患者的位置,然后创建一个chief_complaint(使用不同的MVC),这样当我查看患者表索引时,我会看到他们的主诉。我已将一个complaint_id作为病历表中的外键,如模式文件中所示。但是,我收到以下错误。任何人都可以帮助我吗?
错误消息:nil的未定义方法`name':NilClass ,患者中的NoMethodError #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
答案 0 :(得分:1)
它显示错误消息:nil的未定义方法`name':患者中的NilClass,NoMethodError #index由于可能是数据将为空。所以你可以添加以下条件。
<强>条件强>
<% if patient.name.present? %>
<%= patient.name %>
<% end %>
答案 1 :(得分:1)
答案 2 :(得分:0)
以下行包含错误:
<%= patient.chief_complaint.name %>
您的患者之一未与 chief_complaint 正确关联。
因此,当您查询patient.chief_complaint
时,它会返回nil
。并且nil类没有name
属性。
您可以检查患者是否与chief_complaint相关联。像:
<% if patient.chief_complaint.present? %>
<%= patient.chief_complaint.name %>
<% end %>