I'm trying to get my routes to work like this:
/articles/<category slug>/<article slug>
I'm using:
ruby '2.1.2'
gem 'rails', '4.1.4'
gem "friendly_id", "~> 5.0.1"
I have a category that has many articles
the url structure now is:
/categories/
/articles/
because my routes.rb file looks like this:
resources :categories
resources :articles
my article.rb file:
class Article < ActiveRecord::Base
belongs_to :category
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :globalize]
def slug_candidates
[
:name
]
end
end
here's my category.rb:
class Category < ActiveRecord::Base
has_many :articles
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :globalize]
# Try building a slug based on the following fields in
# increasing order of specificity.
def slug_candidates
[
:name
]
end
end
If I do a nested route like this:
resources :categories do
resources :articles
end
then the structure becomes /categories/<category slug>/articles/<article slug>