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!

(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 %>…

    <% for category in @advert.categories %>
  • <%= link_to category.name, category_path(category) %>
  • <% end %>

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

It helped quite a bit actually. Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs