Displaying last 4 posts...

I have def recent_listings @listing = Listings.order("created_at desc").limit(3) end to grab the last 3 posts, how would i put this on my home page? I tried a listings.each and it didnt work :(

Posted almost 5 years ago by Mike
Posted almost 5 years ago by Alex Yang

Hey Mike, you've got the controller code nearly right (need to make '@listings' plural):

def recent_listings
@listings = Listings.order("created_at desc".limit(3)
end

Then, in your view page, you should use a loop like this:

<% @listings.each do |listing| %>
...
<% end %>

Let me know if that works!

1
Posted almost 5 years ago by Mike

So I can have this work as a page on its own recent_listings.html.erb ... can i render this on the home page? It wouldnt know what to do with listings right?

1
Posted almost 5 years ago by Alex Yang

That's right. You would need to define '@listings' for your home page as well, otherwise your view page wouldn't recognize the variable.

1
Posted almost 5 years ago by Mike

I can't figure it out, I tried different sites and posting to stackoverflow to no avail. I 'm guessing it can be done with homepage in the controller or using a partial. I tried it both ways with setting recent_listings as a :help_method but the homepage doesn't receive the data in the variable.

1
Posted almost 5 years ago by Alex Yang

Take it a step at a time. First, what controller (e.g. ListingsController) and action (e.g. index) are you using for the homepage? Under that controller-action, you need to define the @listings variable. For example:

def index
@listings = Listings.order("created_at desc".limit(3)
end

Then you need to go to your corresponding view page (from this example, views/listings/index.html.erb) and put the code to render the listings there:

<% @listings.each do |listing| %>
# Now you can display what you want, e.g.
# <p><%= listing.name %></p>
<% end %>

Let me know if there are parts that are still confusing to you!

1
Posted almost 5 years ago by Mike

this works, but what if I want to display it on views/pages/homepage?

1
Posted almost 5 years ago by Alex Yang

The same technique should work. Replace 'index' with 'homepage' and you're all set.

1
Posted almost 5 years ago by Mike

when I try view/listings/test.html.erb it works, when I take that code out and put it in view/listings/home.html.erb it says "undefined method `each' for nil:NilClass". So I try to set it as helper_method :home, doesn't help.

1
Posted almost 5 years ago by Alex Yang

Your controller-action has to match the name of your view page. If you're putting the code in the PagesController under the homepage action, the view page should be views/pages/homepage.html.erb.

1
Posted almost 5 years ago by Mike

I don't know why it didn't occur to me to put the method in the pagescontroller, for some reason I thought it had to be in the listingscontroller. Spent hours trying to figure this out hah! Thanks so much man. I only have one more app questions for this app to be complete. Are you familiar with koala gem?

1
Posted almost 5 years ago by Alex Yang

Good - well I'm glad it's working. I've heard of the koala gem but haven't had a chance to use it yet.

1