开发者

What is the best way to deal with Vanity URL helpers in Rails 3?

开发者 https://www.devze.com 2023-01-27 11:43 出处:网络
I have a web application I am work开发者_如何学Going on with Rails 3 and I have just implemented some basic Vanity URL paths to existing resources in the application. What I am looking to do is to not

I have a web application I am work开发者_如何学Going on with Rails 3 and I have just implemented some basic Vanity URL paths to existing resources in the application. What I am looking to do is to not have to explictly build the urls on the user's profile page for the resources that are available, e.g. I would like to be able to build a URL with link_to in the view in the format of:

typealoud.com/:user_id/:thread_id/:comment_id

And not what the standard nested resource helpers give me, something like:

typealoud.com/threads/:thread_id/comments/:comment_id

Should I do this myself as a URL helper, or is there an existing gem?


To do this, I would put this at the top of my routes:

match ':user_id/:thread_id/:id', :to => "comments#show"

I've changed comment_id in this example to id because it's "The Rails Way" that the last id parameter is simply called id. It also results in shorter code.

If you wish to have a routing helper for it use the :as option:

match ':user_id/:thread_id/:id', :to => "comments#show", :as => "comment"

Then you can use comment_path/comment_urlto access the route, but you must pass in three arguments to it, each of them being an object or an id of an object.

0

精彩评论

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