开发者

Stackoverflow like Urls with Rails

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜