开发者

Stackoverflow like Urls with Rails

开发者 https://www.devze.com 2023-03-08 11:55 出处:网络
Most tutorials about prettifying Urls in开发者_开发百科 Rails explain how to get Urls like www.example.org/resource/3-some-sluggish-slug, which is very easy by using the to_param method.

Most tutorials about prettifying Urls in开发者_开发百科 Rails explain how to get Urls like www.example.org/resource/3-some-sluggish-slug, which is very easy by using the to_param method.

What I want is to prettify Urls in a Rails 3 app like Stackoverflow does (www.example.org/resource/3/some-sluggish-slug). The some-sluggish-slug could be any string there, it is just for having a nicer Url and is simply ignored by all logic (like in Stackoverflow). Is there an easy way to do that, too? Or do I have to rewrite the Url somehow before it gets to the routes file or in the routes file itself?


Follow the tutorials, but instead of having a hyphen after the ID, have a /. You will need to write a route to cope with this.

  match 'resource/:id', :to => 'resources#show', :id => /\d+\/[a-z0-9\-]+/, :as => 'public_post'


You will have to define your own routes; see the Rails guide section 3 and onwards.

0

精彩评论

暂无评论...
验证码 换一张
取 消