Shahyan Sajid
Shahyan Sajid

Reputation: 11

Show methods not being called - Rails

I am new to rails and I cannot figure out why methods within my show def, inside of my controller, are not being called.

I am using ActiveRestClient to grab data from my REST API. Everything is working smoothly in my index.html.erb but when I try to move to the next view, I need to make a find call call.

def index
  @t_listings = Listing.all
 #@t_listings = Listing.find(3)
  @listings = Listing.all.paginate({:page => params[:page], :per_page => 10})
end

The 'all' GET request is working fine and the commented out 'find' worked as well. When I try doing the same for show, though, it is not working.

def show
  @listing = Listing.find(params[:id])
 #@listing = Listing.find(3)
end

The page is being routed, since I can see the page in my browser (and in the terminal server logs), but the @listing instance variable contains nothing (as it should).

This is what my console is showing:

Started GET "/listings/4" for ::1 at 2015-04-20 03:21:54 -0400
Processing by ListingsController#show as HTML
  Parameters: {"id"=>"4"}
  Rendered shared/_topnav.html.erb (0.1ms)
  Rendered listings/show.html.erb within layouts/application (4.3ms)
Completed 200 OK in 57ms (Views: 55.9ms | ActiveRestClient: 0.0ms for 0 calls | ActiveRecord: 0.0ms)

As you can see, no ActiveRestClient GET requests are being sent. Here is what my routing file looks like:

resources :listings
get 'listings/:id/delete' => 'listings#delete', :as => :listings_delete

Here is the link_to being used to get to show:

<% @listings.each do |listing|%>
    <span><%= link_to "#{listing.title} - #{listing.company}", "/listings/#{listing.id}", :target => "_blank" %> - <%= listing.city%>, <%= listing.state%></span>

My show.html.erb file is almost completely empty but here it is:

<h1>Listings#show</h1>
<p> Find me in app/views/listings/show.html.erb</p>
<%= @listing.id %>

When I run the above page, I get the error: undefined methodid' for nil:NilClass. This error is on the<% @listing.id %> line.`

Does anyone have any idea what I should do or why this may be the case?

Upvotes: 0

Views: 1160

Answers (1)

Shahyan Sajid
Shahyan Sajid

Reputation: 11

There was a syntax error I made that I cannot believe I had missed. Spent over 6 hours. Turns out I had two show defs. No wonder it wasn't working. Sorry for the trouble everyone. Thank you for your help.

Upvotes: 1

Related Questions