Clo*_*boy 34 anchor routing ruby-on-rails ruby-on-rails-3
想象一下用posts和的博客comments.个人评论的URL可能是posts/741/comments/1220.
但是,我想制作网址posts/741#1220,甚至是posts/741#comment-1230.
这样做最不具侵扰性的方法是什么,所以redirect_to comment_path(my_comment)指向正确的URL?
Hol*_*ust 64
你可以简单地使用
redirect_to post_path(comment.post, :anchor => "comment-#{comment.id}")
Run Code Online (Sandbox Code Playgroud)
使用锚手动构建URL.这样,您仍然可以posts/:post_id/comments/:comment_id在路线中获得评论的绝对URL .您还可以在例如application_controller.rb中创建辅助方法
class ApplicationController
helper :comment_link
def comment_link(comment)
post_path(comment.post, :anchor => "comment-#{comment.id}")
end
end
Run Code Online (Sandbox Code Playgroud)