【发布时间】:2014-02-02 23:01:20
【问题描述】:
在 config/routes.rb 中:
get 'books(/*anything)' => redirect( "/public/%{anything}" ), :format => false
在 spec/requests/store_request_spec.rb 中:
get '/books/aoeu/snth'
expect(response).to redirect_to( '/public/aoeu/snth' )
这失败了:
Failure/Error: expect(response).to redirect_to( '/public/aoeu/snth' )
Expected response to be a redirect to <http://www.example.com/public/aoeu/snth> but was a redirect to <http://www.example.com/public/aoeu%2Fsnth>.
Expected "http://www.example.com/public/aoeu/snth" to be === "http://www.example.com/public/aoeu%2Fsnth".
# ./spec/requests/store_request_spec.rb:14:in `block (3 levels) in <top (required)>'
为什么 %2F 被插入到重定向响应中,我该如何防止它?
编辑:
如果我使用:
get 'books(/*anything)' => redirect( CGI::unescape("/public/%{anything}") ), :format => false
要创建一个未转义的字符串,我仍然会遇到同样的错误。
【问题讨论】:
-
%2F 是正斜杠 (
/) 的 URL 编码。您是否尝试在传递给重定向的字符串上调用html_safe? -
我确实尝试过 html_safe 但没有帮助。这不是 HTML 编码类型问题。
-
呃。我在逃跑方面遇到了很多问题 - 我将查看一些旧代码,看看我是否遇到了这个确切的问题。
标签: ruby-on-rails rspec-rails rails-routing