Newbie needs help with controller

Hey there,

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 hope that I’m making sense.

On 12 January 2016 at 08:42, Alex M. [email protected] wrote:

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).

Colin

Colin L. wrote in post #1180547:

On 12 January 2016 at 08:42, Alex M. [email protected] wrote:

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).

Colin

You’re amazing Colin! It worked perfectly!
You just made my day!!! I can’t thank you enough

On 12 January 2016 at 09:13, Alex M. [email protected] wrote:

[snip]

You’re amazing Colin! It worked perfectly!
You just made my day!!! I can’t thank you enough

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.

Colin