为什么 Rails 编码斜杠来捕获所有(splat)路由?
当使用捕获所有路由时,URL 的正斜杠编码为 %2F
这意味着我无法使用 request.path
查找记录
map.document '*path', :controller => 'documents', :action => 'show'
Page.find_by_permalink('/blog/my_first_post') # Record found
Page.find_by_permalink('blog%2Fmy_first_post') # Record not found
首先为什么会发生这种编码,以及其次有没有办法关掉它?
附言。我知道我可以在查找中使用之前解码 request.path,但我更喜欢一个漂亮的 URL。
When using a catch all route the URLs have the forward slash encoded as %2F
which means I can not lookup a record using request.path
map.document '*path', :controller => 'documents', :action => 'show'
Page.find_by_permalink('/blog/my_first_post') # Record found
Page.find_by_permalink('blog%2Fmy_first_post') # Record not found
Firstly why is this encoding happening, and secondly is there a way to turn it off?
PS. I know I could decode request.path before using in the find but I would prefer a pretty URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 glob 变量重命名为 request_path 并使用 params[:request_path],我做了类似的事情,但从未遇到过这个问题。然而,我相信我剪掉了末端并通过slug进行搜索,然后将路径与我认为应该是的路径进行比较。
try renaming your glob variable to request_path and use params[:request_path], I do a similar thing and I've never had this problem. however, I believe I snip off the end and search by slug, then do a comparison of the path to what I believe it should be.