且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Rails 3中如何在ID中使用锚作为ID?

更新时间:2023-02-21 09:48:39

您可以简单地使用

redirect_to post_path(comment.post, :anchor => "comment-#{comment.id}")

到使用锚点手动构建URL。这样,您仍然可以在路线中将您的评论的绝对URL设为 posts /:post_id / comments /:comment_id 。您还可以在例如application_controller.rb

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.rb

class ApplicationController
  helper :comment_link

  def comment_link(comment)
    post_path(comment.post, :anchor => "comment-#{comment.id}")
  end
end