How I present the data that is in the other table in the view. Rails 5

Asked

Viewed 241 times

-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 a address associated

  • I couldn’t figure out how to fix it ?

2 answers

0

Here’s my form with fields_for

        <%= form_with(model: patient, local: true) do |form| %>
        <% if patient.errors.any? %>
        <div id="error_explanation">
            <h2><%= pluralize(patient.errors.count, "error") %> prohibited this patient from being saved:</h2>

            <ul>
                <% patient.errors.full_messages.each do |message| %>
                <li><%= message %></li>
                <% end %>
            </ul>
        </div>
        <% end %>

        <div class="field">
            <%= form.label :Nome %>
            <%= form.text_field :name, class:"form-control" %>
        </div>

        <div class="field">
            <%= form.label :CPF %>
            <%= form.text_field :cpf, class:"form-control" %>
        </div>

        <div class="field">
            <%= form.label :RG %>
            <%= form.text_field :rg, class:"form-control"  %>
        </div>

        <div class="field">
            <%= form.label :Telefone %>
            <%= form.text_field :phone, class:"form-control"  %>
        </div>

        <div class="field">
            <%= form.label :Data_de_Nascimento %>
            <%= form.text_field :birth, class:"form-control"  %>
        </div>

        <div class="field">
            <%= form.label :Nome_da_Mãe %>
            <%= form.text_field :mother, class:"form-control"  %>
        </div>

        <%= form.fields_for :address do |address_fields|%>

        <div class="field">
            <%= form.label :Rua %>
            <%= address_fields.text_field :street, class:"form-control"  %>
        </div>

        <div class="field">
            <%= form.label :CEP %>
            <%= address_fields.text_field :zipcode, class:"form-control"  %>
        </div>
        <div class="field">
            <%= form.label :Cidade %>
            <%= address_fields.text_field :city, class:"form-control"  %>
        </div>
        <div class="field">
            <%= form.label :Estado %>
            <%= address_fields.text_field :state, class:"form-control"  %>
        </div>
        <div class="field">
            <%= form.label :Bairro %>
            <%= address_fields.text_field :neighborhood, class:"form-control"  %>
        </div>
        <%end%>

        <div class="actions">
            <%= form.submit 'Cadastrar', :class=>'btn btn-success' %>
        </div>
        <% end %>
  • Did you clean/correct the database after the last correction? Your form seems correct. In addition, this field is for answers. Your problem now seems to be being while saving the form data. I suggest opening another error-related question while saving the data. I will edit my previous reply with a workaround of your problem. But it would be a stopgap

  • Because it is added the if Patient.address in the code as put on Edit. Now it returns the street field but blank. Which I filled out when I registered the patient.

  • Yes, I dropped the bank and created again.

0

The cause of the mistake is that Patient that you’re trying to show off doesn’t have a Address.
I mean, he is nil and so when you try to access the attribute street of address you have the error

Undefined method `street' for nil:Nilclass

Somehow you allowed records to be saved in the bank without this association.

To avoid this problem, you can choose to oblige that a Patient be created with only one Street associated.

Your model would look something like this

class Patient < ApplicationRecord
  has_one: address
  validates :address, presence: true
end

EDIT:

Depending on the business rule, I believe the ideal solution would be the previous case. But as a measure to circumvent the problem, you could display the field only if it exists.

                  <p>
                    <strong>Rua:</strong>
                    <%= patient.address.street if patient.address %>
                </p>
  • Okay. I corrected for what you gave me, the only difference is that I have accepts_nested_attributes_for :address in this model of mine. But now it does not save Patient, in the same form it returns the following error: 1 error prohibited this Patient from being saved: Address can’t be Blank

  • So. Still you’re trying to save Patient with a blank address. If you have an accpets_nested_attributes_for :address. In your Patient creation form you also need to create an address directly. That is, pass the data of this Patient address.

  • Using a fields_for in your creation form. So Rails will send an address_param along with the Patient parameters. As asked in this question. https://stackoverflow.com/questions/19274730/rails-form-with-nested-attributes-accepts-nested-attributes-for

  • But I’ve been using fields_for since I created the form. I don’t understand why you’re not saving my form here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.