Ask A Question

Notifications

You’re not receiving notifications from this thread.

Why this error ? how I fix this.

Thomaz Wanderbruck Schmidt asked in Rails

I have a form that has fields from two different tables, the Patients table and the Addresses table. Please make a search field to use only one patient (Patient) and return his data, up to the fields in the patient table and return the right data when adding a field to present a street in case of a street and it will be the following error:

NoMethodError in Backend::Search#patients

Showing /home/thomazws/Documentos/Rails/projetohroV2/app/views/backend/search/patients.html.erb where line #106 raised:

undefined method `street' for nil:NilClass

Extracted source (around line #106):

<p>
<strong>Rua:</strong>
<%= patient.address.street %>
</p>

I have a controller for Search with this code:

class Backend::SearchController < ApplicationController

  def patients
    @patients = Patient.where(cpf: params[:q])
  end

end

and my view:

  <% @patients.each do |patient| %>

                     <p>
                        <strong>Nome:</strong>
                        <%= patient.name %>
                    </p>


                      <p>
                        <strong>CPF:</strong>
                        <%= patient.cpf %>
                    </p>

                      <p>
                        <strong>RG:</strong>
                        <%= patient.rg %>
                    </p>

                      <p>
                        <strong>Telefone:</strong>
                        <%= patient.phone %>
                    </p>

                      <p>
                        <strong>Data de Nascimento:</strong>
                        <%= patient.birth %>
                    </p>

                      <p>
                        <strong>Nome da Mãe:</strong>
                        <%= patient.mother %>
                    </p>

                      <p>
                        <strong>Rua:</strong>
                        <%= patient.address.street %>
                    </p>
                <%end%>
Reply

To help me troubleshoot better, can you share all the attributes for the Patient Class and share the entire code for Patient model?

Reply

Yes here.

Model Patient:

class Patient < ApplicationRecord
    has_one :address

    accepts_nested_attributes_for :address
end

MIgration Patient:

class CreatePatients < ActiveRecord::Migration[5.2]
  def change
    create_table :patients do |t|
      t.string :name
      t.string :cpf
      t.string :rg
      t.string :phone
      t.string :birth
      t.string :mother

      t.timestamps
    end
  end
end

My Search Controller

class Backend::SearchController < ApplicationController

  def patients
    @patients = Patient.where(cpf: params[:q])
  end

end

Need more information ? Helpme pleaase hahahaha xD

Reply

From the code, most likely that patient does not have an address. Have you tried using byebug or pry to take a look?

Reply

No, I doesn't know this.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.