I’m trying to set up rails to use both the ID and the Handle (which is just an URL safe version of the title) of a blog post in the route.
match '/articles/:id/:handle', :to => 'articles#show'
resources :articles
This works, of course — but I can’t seem to set up the to_param method in the model os the longer URL — with the handle attached, is the default.
This doesn’t work (not that I really expected it to):
def to_param
"#{id}/#{handle}"
end
I get a No route matches {:action=>"edit", :controller=>"articles", error. I also tried just using the handle, but then Rails generates links to the resource just using the handle and not the ID. I know I can do it with a – in stead of a /, but I prefer the /. Any way to make this work? If I have to add some extra paremeters to my link_to helpers, that’s okay.
Did you try to pass a
Hashtolink_to?Update
You have to modify your routes:
and use the following helper to generate the link:
You can override the helper to simplify things:
and use it like this: