I'm building a blog and I want my categories to have beautiful URL's like
blog/music
, blog/art
, etc.
So far, I've managed to make them look like this
/blog?category=music
class Article < ActiveRecord::Base
belongs_to :category
extend FriendlyId
friendly_id :title, use: :slugged
scope :by_category, -> (slug) { joins(:category).where('categories.slug = ?', slug) if slug }
end
-
class Category < ActiveRecord::Base
has_many :articles
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
end
view
= active_link_to category.name, articles_path(category: category.slug), remote: true,
class_active: 'active', class_inactive: 'inactive', wrap_tag: :dd, active: /#{Regexp.escape(category=category.slug)}/
controller
Article.by_category(params[:category])