Problem with "Undefined method `image1' for nil:Nilclass"

Asked

Viewed 42 times

0

I am developing a real estate site in Ruby on Rails and I ended up having problems when I created a route called realtors ( Rails g controller Realtor index), serving to agglutinate all the brokers registered and thus get the profiles that are in agent_details.html.erb (folder properties).
I don’t know why you are giving Nilclass in everything that has @agent. What should I put in the controller of the High not to give error? I’ve tried everything.

On the index of realtors I declared:

<%= render "properties/agent_details", properties: @accounts %>

I went to trial and error several times to see if I could, but the common mistake is Undefined method `image1' for nil:Nilclass

agent_details.html.erb:

 <div class="row">
   <div class="col-12 pr-1 pl-1">
      <button class="btn btn-block btn-info text-uppercase mb-3" id="show-tel" data-telephone="<%= @agent.telephone %>"><i class="fas fa-phone mr-2"></i><span>Call</span></button>
     </div>
   </div>

<div class="row">
  <div class="col-md-6 pr-1 pl-1 pb-1">
    <%= link_to "Profile", profile_path(@agent.id), class: "btn btn-secondary btn-block text-uppercase" %>
    </div> 
      <div class="col-md-6 pl-1 pr-1">
        <button class="btn btn-block btn-success text-uppercase" data-toggle="modal" data-target="#contact-modal">Email</button>
      </div> 
    </div>
 </div>

What should I declare in realtors_controller.Rb Why not make a mistake? I know that in Activerecord the classes declared for agent, such as image1, Telephone and full_name belong to the Account:

add_image1_to_account.Rb

class AddImage1ToAccount < ActiveRecord::Migration[6.0]
  def change
    
    add_column :accounts, :company, :string
    add_column :accounts, :image1, :string
  end
 
end

add_telephone_to_account.Rb

class AddTelephoneToAccounts < ActiveRecord::Migration[6.0]
  def change
    add_column :accounts, :telephone, :string
  end
end

Account.Rb

class Account < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable



  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :trackable, :confirmable
   
         has_attached_file :image1, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/img"
         validates_attachment_content_type :image1, content_type: /\Aimage\/.*\z/       
   
         has_attached_file :coverimg, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/img"
         validates_attachment_content_type :coverimg, content_type: /\Aimage\/.*\z/ 

  has_many :properties
 
  def full_name
    "#{first_name} #{last_name}"
  end
 
end

I hope you have understood my doubt and I thank you for your help!

No answers

Browser other questions tagged

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