Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Use slug instead of id on resource routes

So I'm looking for a solution that would help me achieve the following with Rails resource:

/admin/articles/:slug/edit

as opposed to

/admin/articles/:id/edit

I'm looking for Rails resource routes and not the other kinds of routes.

Just wanted to know if it was possible. If so, how?

like image 339
sidegeeks Avatar asked Jun 25 '15 20:06

sidegeeks


1 Answers

# config/routes.rb
resources :articles, param: :slug

In the terminal:

$ rake routes
...
article GET    /articles/:slug(.:format)      articles#show
...
like image 105
Thomas Klemm Avatar answered Sep 21 '22 15:09

Thomas Klemm