Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR nested categories

folks! I wonder how to make url like: site.com/coding/ruby/rails/article-name As you can see there is nested category. I've already looked at acts-as-tree and awesome nested sets, but it makes urls like site.com/rails/article-name.

So, please help me

like image 592
Fluffy Avatar asked Aug 01 '26 19:08

Fluffy


1 Answers

If you want to use a resource-ful controller, make a route like this:

scope '(*categories)' do
  resources :articles
end

this will require using 'article' at the end of your urls: /coding/ruby/rails/articles/article-name

or you can use a route like this:

match '(*categories)/:id' => 'articles#show', :as => :article

this will let you do article_path(:categories => 'coding/ruby/rails', :id => article.friendly_id)

to get /coding/ruby/rails/article-name

If you are using awesome_nested_set, you could generate the categories string by doing something like

article.category.self_and_ancestors.join("/")
like image 118
Michael Johnston Avatar answered Aug 03 '26 09:08

Michael Johnston