roflkk
2010-Feb-22 15:51 UTC
Need help understanding how to sort and view items through categories
This is probably an easy guess for some, but after searching the web for quite some time, I''ve concluded that I need help understanding this. My case: I''m creating an ad application where people can sell and buy items through their own ads. An ad belongs to one category, and therefore the question: When a user views all the current ads as a list (webisteurl/ads) - how do I make the categories show up next to that list like a clickable sorting menu? Help would be very much appreciated! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries
2010-Feb-22 21:20 UTC
Re: Need help understanding how to sort and view items through categories
> > This is probably an easy guess for some, but after searching the web > for quite some time, I''ve concluded that I need help understanding > this. > > My case: > > I''m creating an ad application where people can sell and buy items > through their own ads. An ad belongs to one category, and therefore > the question: When a user views all the current ads as a list > (webisteurl/ads) - how do I make the categories show up next to that > list like a clickable sorting menu? >I''m not 100% sure which part you''re stuck on. To get the association set up you need to look in to ActiveRecord Relationships, in particular has_and_belongs_to_many. class Advert < ActiveRecord::Base has_and_belongs_to_many :categories end Then in the view do something along the lines of: ...<%= @advert.content %>... <ul> <% for category in @advert.categories %> <li><%= link_to category.name, category_path(category) %></li> <% end %> </ul> You''d have a category controller; maybe using restful routes or a named route such as: map.category "/category/:id", :controller => "categories", :action => "show" In that controller you''d do: @articles.find_by_category(params[:id]) And iterate over those articles in the view. Depending on where you were stuck, hopefully that''s given you some hints to get you moving. If not, post back with more details. Cheers, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
roflkk
2010-Feb-24 22:38 UTC
Re: Need help understanding how to sort and view items through categories
It helped quite a bit actually. Thanks! On Feb 22, 10:20 pm, Andy Jeffries <andyjeffr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > This is probably an easy guess for some, but after searching the web > > for quite some time, I''ve concluded that I need help understanding > > this. > > > My case: > > > I''m creating an ad application where people can sell and buy items > > through their own ads. An ad belongs to one category, and therefore > > the question: When a user views all the current ads as a list > > (webisteurl/ads) - how do I make the categories show up next to that > > list like a clickable sorting menu? > > I''m not 100% sure which part you''re stuck on. To get the association set up > you need to look in to ActiveRecord Relationships, in particular > has_and_belongs_to_many. > > class Advert < ActiveRecord::Base > has_and_belongs_to_many :categories > end > > Then in the view do something along the lines of: > > ...<%= @advert.content %>... > > <ul> > <% for category in @advert.categories %> > <li><%= link_to category.name, category_path(category) %></li> > <% end %> > </ul> > > You''d have a category controller; maybe using restful routes or a named > route such as: > > map.category "/category/:id", :controller => "categories", :action => "show" > > In that controller you''d do: > > @articles.find_by_category(params[:id]) > > And iterate over those articles in the view. > > Depending on where you were stuck, hopefully that''s given you some hints to > get you moving. If not, post back with more details. > > Cheers, > > Andy-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.