I have a web application I am working 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:
I’ve changed
comment_idin this example toidbecause it’s “The Rails Way” that the last id parameter is simply calledid. It also results in shorter code.If you wish to have a routing helper for it use the
:asoption: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.