Want To Add A Page Where Buyers Can See All Items Listed By A Certain Seller

Looking to add a Vendors page. Essentially if I like a certain seller. I want to be able to see only products by that seller. It will essentially be a vendor page for that Seller specifically where they can add a banner and a brief bio. How do I go about this?

Posted almost 6 years ago by Patrick Walsh
Posted almost 6 years ago by Alex Yang

Like with any new feature, the basic outline is as follows:

1. Set up any new URLs you'll need in config/routes.rb
2. Add new action(s) corresponding to those new URLs in your controller
3. Add view pages corresponding to those new URLs in your 'views' folder
4. Take care of any residual changes or ripple effects (e.g. add a button to your new URL)

In this case, you could set up a new URL at WEBSITE.com/listings/USER_ID by adding "get '/listings/:id', to: 'listings#vendor'.

Then you'll need to add this 'vendor' action to your Listings controller, making sure to set the '@listings' variable equal to all the Listings created by the User with the ID given in the URL. This could be something like '@listings = Listing.where(seller: User.find(params[:id]))'.

Then you'll need to create a vendor.html.erb view page to display all the Listings contained in the @listings variable that you defined in your controller.

Finally, you can add a button to go to a specific User's page with something like this: '<%= link_to "Go to vendor page", vendor_path(@seller.id) %>'

Posted over 5 years ago by Amy Peterson

Hi Alex,
I'm not clear on what I need to put into my routes.db and listings_controller files for this? In my case I created a new view page called "shop.html.erb" instead of calling it a "vendor" page.
Thanks, Amy

Posted over 5 years ago by Alex Yang

Just replace anything with 'vendor' with 'shop'. For example, in your config/routes.rb file, write something like this:

get '/listings/:id', to: 'listings#shop'

And then in your listings_controller.rb file, add a 'shop' action that should look like this:

def shop
@listings = Listing.where(seller: User.find(params[:id]))

Posted over 5 years ago by Amy Peterson

thanks for the quick reply -- i did that originally and i was getting the error:
ActiveRecord::RecordNotFound in ListingsController#shop
Couldn't find User with 'id'=23
and now when i click on a listing on the home page, before i was taken to the listing show view file, and now i don't go to the show view file...instead i'm getting this error.

Posted over 5 years ago by Alex Yang

You're seeing the error because you're trying to access the 'shop' page for a User that doesn't exist. Since there's no User with an ID of 23, the corresponding 'shop' page doesn't exist either. Try using a user ID that exists instead, and double-check the route of the link to see where it's supposed to take you after clicking on a Listing.