ruby-on-rails - 在Rails 3中,如何在ID中使用 anchor 作为ID?

标签 ruby-on-rails ruby-on-rails-3 routing anchor

想象一下一个带有postscomments的博客。单个评论的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.rb
class 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/

相关文章:

ruby - 导轨 3 : How to store screen resolution?

ruby-on-rails - Rails 3 中奇怪的 DoubleRenderError

docker - docker路由没有iptables

android - 带 map 和路线的 Android 项目要使用哪些库?

php - 如果尝试进入登录屏幕,Laravel 5 会重定向到主页

ruby-on-rails - 为 mongoid 哈希字段设置默认哈希键

jquery - (ruby on Rails)服务器数据更新后,如何使用 jquery/ajax 以最快的方式从部分加载/显示特定 div?

ruby-on-rails - Rails,在创建新记录之前删除存在具有相同列值的记录

c++ - 按原样保存用户输入-Ruby on Rails

ruby-on-rails - 为新的 ActiveRecord 验证指定错误消息(存在)