routes - Rails - SEO friendly URLs with Self joined models -


right there 2 models: category , product. categories self joining make sub_categories. category has_many product, of course belongs_to category.

the question have this: how map routes these resources have seo friendly urls.

for example: if user clicks on "lamas" category, clicks on "maintenence" sub-category, , clicks on "lama polish" product. path be: http://lamasareawesome.com/categories/1/categories/26/products/11

what want is: http://lamasareawesome.com/lamas/maintenence/lama-polish

so far im not having luck trimming controller name out of path, , replacing model attribute.

creating routes use attribute id pretty trivial. friendlyid great out of box solution urls like:

/categories/lamas/categories/maintenance/products/lama-polish 

creating url's such lamas/maintenence/lama-polish possible difficult since not conventional , there many potential pitfalls.

you example start out with:

resources :categories, path: '/'   resources :categories,  path: ''     # resources :products    end end 

which create:

                prefix verb   uri pattern                      controller#action    category_categories    /:category_id(.:format)          categories#index                        post   /:category_id(.:format)          categories#create  new_category_category    /:category_id/new(.:format)      categories#new edit_category_category    /:category_id/:id/edit(.:format) categories#edit      category_category    /:category_id/:id(.:format)      categories#show                        patch  /:category_id/:id(.:format)      categories#update                        put    /:category_id/:id(.:format)      categories#update                        delete /:category_id/:id(.:format)      categories#destroy             categories    /                                categories#index                        post   /                                categories#create           new_category    /new(.:format)                   categories#new          edit_category    /:id/edit(.:format)              categories#edit               category    /:id(.:format)                   categories#show                        patch  /:id(.:format)                   categories#update                        put    /:id(.:format)                   categories#update                        delete /:id(.:format)                   categories#destroy 

but there supersized gotcha - let's request

get /lamas/joe-the-lama 

how rails supposed know request should handled lamascontroller , not categoriescontroller? have database query both. of course not issue if have 2 categories drift - things going complicated fast.

the standard rails style restful routing may bit wordy avoid lot of potential ambiguities.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -