想象一下一个带有posts
和comments
的博客。单个评论的URL可能是posts/741/comments/1220
。
但是,我想将URL设置为posts/741#1220
甚至posts/741#comment-1230
。
什么是最侵入性的方法,以便redirect_to comment_path(my_comment)
指向正确的URL?
最佳答案
您可以简单地使用
redirect_to post_path(comment.post, :anchor => "comment-#{comment.id}")
以使用 anchor 手动构建URL。这样,您仍然可以在 route 使用
posts/:post_id/comments/:comment_id
作为注释的绝对URL。您还可以在例如application_controller.rbclass ApplicationController
helper :comment_link
def comment_link(comment)
post_path(comment.post, :anchor => "comment-#{comment.id}")
end
end
关于ruby-on-rails - 在Rails 3中,如何在ID中使用 anchor 作为ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4981029/