Scott S.
Scott S.

Reputation: 759

undefined method `page_leads' for nil:NilClass

I've been banging my head against this for a while. I know it's a simple problem, and I've reviewed other code examples where I'm doing this successfully, but, I'm completely stumped.

I'm getting an error "undefined method `page_leads' for nil:NilClass" when I try to go to the "Show" page. On my landing_pages "show" page I'm trying to show the leads that came in via that page.

My show page code for this is:

<table class="table table-striped">
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Score</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @landingpage.page_leads.each do |page_lead| %>
      <tr>
        <td><%= page_lead.fname %></td>
        <td><%= page_lead.lname %></td>
        <td><%= page_lead.score %></td>
        <td><%= link_to 'Show', page_lead %></td>
        <td><%= link_to 'Edit', edit_page_lead_path(page_lead) %></td>
        <td><%= link_to 'Destroy', page_lead, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

My landing_page model has:

has_many :page_leads

My page_lead model has:

belongs_to :landing_page

My controller code for the "show" method for both landing_pages and page_lead is:

def show  
  @landing_page = LandingPage.all(landing_page_params) 
end

On my page_leads table I have the "landing_page_id" field so I can associate the landing page to the lead.

Any ideas what I'm doing wrong?

Thanks in advance

Upvotes: 0

Views: 78

Answers (2)

mdemolin
mdemolin

Reputation: 2534

Your controller action does not load any instance of a model. You expect it to load an instance of a LandingPage (usually the one in params[:id] for a show action). So your controller should assign it:

# `GET /landing_pages/:id`
def show
  @landingpage = LandingPage.find( params[:id] )
end

Upvotes: 2

fab
fab

Reputation: 315

It is because your @landingpage instance variable is not defined when you run that code.

Basically, in your controller's action, you should have something like:

def show
  @landinpage = ... # insert your definition here
  # rest of your controller's action
end

Upvotes: 1

Related Questions