Imagine a blog with posts
and comments
. An individual comment's URL might be posts/741/comments/1220
.
However, I'd like to make the URL posts/741#1220
, or even posts/741#comment-1230
.
What's the least intrusive way of doing this, so that redirect_to comment_path(my_comment)
points to the correct URL?
You could simply use
to manually build the URL with the anchor. That way, you can still have the absolute URL to your comments as
posts/:post_id/comments/:comment_id
in your routes. You can also create a helper method in e.g. application_controller.rbPrefer to keep your anchor builder in one place.
then
Adding the
created_at.to_i
obscures your data a bit more and doesn't harm anything.you could override the method to_param in comment to do that.
for example
And you'll have to tweak routes.rb. Check this blog for more information.