简体   繁体   中英

Ruby On Rails - Undefined Method error

I am trying to make it to where I create a patient and then I create a chief_complaint (using a different MVC) so that when I view the patient table index I will see their chief complaint. I have made a complaint_id as a foreign key in the patient table as seen in the schema file. However, I get the error below. Can anyone help me out?

Error messages: undefined method `name' for 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

it shows Error messages: undefined method `name' for nil:NilClass , NoMethodError in Patients#index Because of May be Data will be Blank. So you can add Below condition.

Condition

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

try this short method without using if

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

You can check the use of try here

The following line contains the error:

<%= patient.chief_complaint.name %>

One of your patient is not properly associated with the chief_complaint . Thus when you query for patient.chief_complaint it returns nil . And a nil class doesn't have a name property.

You can check if patient associated with a chief_complaint or not. Like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM