All threads / Why this error ? how I fix this.

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%>

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

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

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.