I've set up a my site so that my users can create individual product collections and add their listings to each collection. I then created a site page that shows all created collections by all users. Right now I have the collection name and the "by so-and-so" displaying there for each collection, all good. I now want to show the first 3 listing images for each collection on top. I am able to get the first 3 images from my listings database to show in each collection using @listings = Listings.all, but that's not correct. But at least that confirms everything connects. I'm having trouble with my controller to def the @listings by 'collection_id'. I've tried so many different variations and the one below is where I'm currently stuck and serves a blank collection and doesn't recognize any/or each collection's listings.
My Pages Controller:
@collections = Collection.all.order("created_at DESC")
@listings = Listing.where(collection_id: @collection)
My Collections View Page:
<% @collections.each do |collection| %>
<% @listings.limit(3).each do |listing| %>
<%= image_tag listing.image.url(:small) %>
<% end %>
<%= collection.name %>
<%= collection.user.business_name %>
<% end %>
What am I missing in my controller to get the images to show by :collection_id? Do I need to change anything in my view page? Additionally, is it okay to nest the @listings.limit(3).each within my @collections?
Aucun commentaire:
Enregistrer un commentaire