Newbie needs help with e-commerce site

19 views
Skip to first unread message

Alexis Marie

unread,
Jan 12, 2016, 7:40:36 AM1/12/16
to Ruby on Rails: Talk
Hey there,

I'm super new to Ruby on Rails. Currently I'm currently working on an e-commerce project. (I'm building it for practice).
My website has buyers and sellers. Each seller has their own page. How can set it up where you can see all the items listed by on seller on their page?


Here is my listings controller:

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


URL in my config/routes.rb

 get '/shop/:id' => 'listings#shop', as: 'shop'


Walter Lee Davis

unread,
Jan 12, 2016, 8:00:28 AM1/12/16
to rubyonra...@googlegroups.com

> On Jan 12, 2016, at 3:28 AM, Alexis Marie <alexismari...@gmail.com> wrote:
>
> Hey there,
>
> I'm super new to Ruby on Rails. Currently I'm currently working on an e-commerce project. (I'm building it for practice).
> My website has buyers and sellers. Each seller has their own page. How can set it up where you can see all the items listed by on seller on their page?

The key to this is your associations. If you have set up something like this:

class Seller
has_many :listings
end

class Listing
belongs_to :seller
end

>
>
> Here is my listings controller:
>
> def shop
> @listings = Listing.where(seller: User.find(params[:id,]))
> @user = User.find(params[:id])
> end

I'm not entirely clear who @user is meant to be here -- is this the seller? And you keep throwing around params[:id]. If this is the listings controller, :id should be the ID of a particular Listing, not a User. If you were using a UsersController#show method (or, to match my models above, a SellersController#show method, because you are "showing" a particular Seller), then you would have already loaded the @seller, and could simply call @seller.listings to get the collection in one step. After you have that working, then you could optimize it with an eager association:

@seller = Seller.find(params[:id])
@listings = @seller.listings

becomes

@seller = Seller.includes(:listings).find(params[:id])
@listings = @seller.listings

And that will save you the heartache of an N+1 query.

>
>
> URL in my config/routes.rb
>
> get '/shop/:id' => 'listings#shop', as: 'shop'
>
>

There's a great explanation of Rails' routing system, with particular emphasis on REST and Why It Is Good at http://guides.rubyonrails.org/routing.html

Also, if you haven't done so already (and your controller example hints to me that maybe you haven't) please give yourself a huge stepladder against the learning curve by visiting http://railstutorial.org and working all the way through the tutorial (it's free to use online). I had been using Rails professionally for nearly 3 years (and programming for many more years before that) when I did this, and I learned plenty. It will take you a few days, but you will save yourself literally years of wasted time in the future.

Walter

>
> --
> 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/fd88c9e1-eb40-4822-be65-0f3047fa8cf0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages