I Want to Create an Individual User Shop Page

On my home page I have all listings from all users. When I click on a listing I'm currently taken to the show page for that listing that shows the name of the user, the description, and the "buy now" button, etc.. I'd like to be able to click on the name of the user from the show page and go to a shop page for that user that lists all of the items from that same user. Then like the home page I want to still be able to click on a listing in the shop page and view the show page so that I can purchase the item from the shop page as well as from the home page. I've created a new page called "shop.html.erb". I need help with what to put in my routes.rb file, my listings_controller file, and finally what should the 'link_to' be on the show page. Thanks in advance!

Posted over 4 years ago by Amy Peterson
Posted over 4 years ago by Amy Peterson

Sorry, I just want to clarify:

I want to click on a homepage listing, go to listing show page, then go to shop page and view all listings for that User.

1
Posted over 4 years ago by Amy Peterson

Hi Alex, yes, I'm sorry for the repeat questions, it's similar, yes. Except the other thread was bypassing my show page and I was getting additional errors there I couldn't debug. Here's what I have done so far from the other thread:

Added to my ROUTE file:
get '/listings/:id' => 'listings#shop', as: 'shop'

Added to my LISTINGS_CONTROLLER:
def shop
@listings = Listing.where(user: Listing.find(params[:id]))
@user = User.find(params[:id])
end

added to my SHOW page:
<%= link_to "#{@listing.user.name}", shop_path %>

added to my SHOP file:
<%= @user.name %>
<% @listings.each do |listing| %>
<%= link_to image_tag(listing.image.url), listing %>
<%= listing.name %>
<%= number_to_currency(listing.price) %>
<%= number_to_currency(listing.sale_price) %></p>
<% end %>

So overall, I have 2 users. User 1 has 4 listings (id: 1-4), User 2 has 3 listings (id: 5-7). Total 7 listings on homepage.

ONLY when I click on the first (id 1) and second (id 2) listings created by User 1 does the shop page come up (bypassing show page) and it does display just the 4 listings by User 1. When I click on any listing id 3 or beyond on the home page, I get this error:
"Couldn't find User with 'id'=5"

It looks like it's only matching up the user id when the listing id is 1 or 2, the same as the only 2 user ids present in the site.

I know my code is incorrect at the moment, but I just don't know what to adjust and where, in order to get the results I want, which is: I don't want to bypass the show pages. I want to click on a homepage listing, go to listing show page, then go to shop page and view all listings for User 1.

1
Posted over 4 years ago by Alex Yang

It sounds like the issue is that the links aren't taking you to the right pages, so you should take a closer look at your link routes. For example, when you click on a User's name on the Show page, how does your app know where to go? It needs to know the User ID so it can look up all the Listings that belong to that User.

I just realized that the code I gave you earlier (get '/listings/:id', to: 'listings#shop') won't work because the URL format /listings/:id is identical to the format we use for our Show pages. You'll need to use a new format (e.g. /shop/:id). You'll also need to provide the User ID in the link route so your app has the information it needs. For example:

<%= link_to "#{@listing.user.name}", shop_path(@listing.user.id) %>

Try that out and let me know how it goes.

1
Posted over 4 years ago by Amy Peterson

That worked : ) Thank you so much Alex. I can't tell you how much I appreciate all that you do and for bringing us BaseRails to learn.

1
Posted over 4 years ago by Alex Yang

Thanks for the kind words - you're very welcome!

1
Posted over 4 years ago by Alex Yang

Is this the same feature you were trying to build from this thread?
https://www.baserails.com/questions/want-to-add-a-page-where-buyers-can-see-all-items-listed-by-a-certain-seller

Other than creating a shop.html.erb file, what other progress have you made?

1