Newbie needs help with controller

22 views
Skip to first unread message

Alex Marie

unread,
Jan 12, 2016, 3:43:42 AM1/12/16
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 12, 2016, 3:54:13 AM1/12/16
to Ruby on Rails: Talk
On 12 January 2016 at 08:42, Alex Marie <li...@ruby-forum.com> wrote:
> 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

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

>
>
>
> I hope that I'm making sense.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ec88a5afaf60d0a3ed6a0fe654eb640d%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Alex Marie

unread,
Jan 12, 2016, 4:00:30 AM1/12/16
to rubyonra...@googlegroups.com
You're amazing Colin! It worked perfectly!
You just made my day!!!!

Alex Marie

unread,
Jan 12, 2016, 4:14:20 AM1/12/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1180547:
> On 12 January 2016 at 08:42, Alex Marie <li...@ruby-forum.com> 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

Colin Law

unread,
Jan 12, 2016, 4:16:27 AM1/12/16
to Ruby on Rails: Talk
On 12 January 2016 at 09:13, Alex Marie <li...@ruby-forum.com> 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
Reply all
Reply to author
Forward
0 new messages