-2
I have a form that has fields from two different tables Patients table and Addresses table. However I did a search field to just search a patient(Patient) and return his data, until the fields of the table Patients it returns the right data but when I add a field to present the street in the street case it returns me 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 that searches with the following code:
class Backend::SearchController < ApplicationController
def patients
@patients = Patient.where(cpf: params[:q])
end
end
and here is the view in which I present the data
<% @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%>
Apparently this instance of
patient
does not have aaddress
associated– Wilker
I couldn’t figure out how to fix it ?
– Thomaz Wanderbruck Schmidt