Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean URLs Using forward slash '/' in to_param with Rails (3)

Is this possible?

def to_param
  "#{id}%2F#{slug}"
end

This works in Chrome and Safari, but if Firefox you see the "%2F" in the address bar. Is there a cleaner way?

like image 953
Nathan Wienert Avatar asked Dec 08 '22 02:12

Nathan Wienert


1 Answers

This is indeed an old post, but I want to build a bit on it.

If you don't want to have to handle an slug variable in your params, you really need to define that method to_param in your model

def to_param
  "#{id}/#{title}"
end

and set a route like so:

resources :posts, :id => /[0-9]+\/.+/

That way your link definition looks quite like a normal one:

link_to post.title, post_url(post)

Very simple: http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash

like image 52
miguelsan Avatar answered Feb 12 '23 18:02

miguelsan