0

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?

  • 1
    Have you checked routing if you have used `resource` or `resources`. you can check it using `rake routes` in command prompt as well. Let me know if you get show method in there. – Manish Shrivastava Apr 20 '15 at 07:42
  • What URL you are hitting for SHOW? – RAJ Apr 20 '15 at 07:42
  • @ManishShrivastava Yes, I am getting the show method: listing GET /listings/:id(.:format) listings#show – Shahyan Sajid Apr 20 '15 at 07:46
  • What's in your view? – Marek Lipka Apr 20 '15 at 07:46
  • @RAJ I am hitting "/listings/#{listing.id}" I only need the ID param so I figured that should be good enough? – Shahyan Sajid Apr 20 '15 at 07:49
  • @MarekLipka This is the tag used in the index view to link to show: <%= link_to "#{listing.title} - #{listing.company}", "/listings/#{listing.id}", :target => "_blank" %> - <%= listing.city%>, <%= listing.state%> My show view is pretty much empty – Shahyan Sajid Apr 20 '15 at 08:06
  • Why do you use `listing` instead of `@listing` in your view? Why don't you use route helpers? – Marek Lipka Apr 20 '15 at 08:07
  • @MarekLipka Index is working fine. I am using `listing` instead of `@listing` because my full code looks like this: `<% @listings.each do |listing|%>
    <%= link_to "#{listing.title} - #{listing.company}", "/listings/#{listing.id}", :target => "_blank" %> - <%= listing.city%>, <%= listing.state%>` I do not what route helpers are (since I am new). Could you give me some guidance about how I could use them and how they could help?
    – Shahyan Sajid Apr 20 '15 at 08:16
  • So the next question, why do you use `@listings` instance variable in `show` action? – Marek Lipka Apr 20 '15 at 08:18
  • @MarekLipka The `show` action is supposed to give me the information for one `listing` because the index view shows all of the listings. Now when I click on the link, I want to go to a page which shows me details for one listing. I am only using `@listing` in show and not `@listings` – Shahyan Sajid Apr 20 '15 at 08:25
  • Ok, so show me `show.html.erb` instead of `index`. – Marek Lipka Apr 20 '15 at 08:26
  • @MarekLipka show is almost empty right now: `<%= render 'shared/topnav' %>

    Listings#show

    Find me in app/views/listings/show.html.erb

    <%= @listing.id %> <%= render 'shared/footer' %>` I get an error when I run this though: _undefined method `id' for nil:NilClass_
    – Shahyan Sajid Apr 20 '15 at 08:31

1 Answers1

1

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.