I'm super new to RoR. I've been working on an eCommerce project for
practice. Currently my website has buyers and sellers. Each seller has
personal page.
I'm trying to figure out how to show each seller's listings on their
personal page. I don't know what to put in my controller
Here's what I got so far:
def shop
@listings = Listing.where(seller: User.find(params[:id,]))
@user = User.find(params[:id])
end
I'm super new to RoR. I've been working on an eCommerce project for
practice. Currently my website has buyers and sellers. Each seller has
personal page.
I'm trying to figure out how to show each seller's listings on their
personal page. I don't know what to put in my controller
Here's what I got so far:
def shop
@listings = Listing.where(seller: User.find(params[:id,]))
@user = User.find(params[:id])
end
Better to use
@user = User.find(params[:id])
@listings = @user.listings
That probably won't work for you at the moment as you may not have set
up the associations properly. Also, assuming the user has to logon
then you should not be passing the id in the params but should be
saving it in the session when the user logs on, in which case you
would use
@user = current_user
I suggest that you work right through a good tutorial such as
railstutorial.org (which is free to use online) which will show you
the basics of rails. Also study the Rails Guide on ActiveRecord
Associations (and the other Rails Guides).
Glad to be of help, but please do work through that tutorial. If you
missed that bit of the magic of rails then no doubt you have missed
other equally useful features that will make your like easier.