如何在没有Rails 4的情况下向所有URL添加trailing_slash?

Luc*_*cia 4 url ruby-on-rails url-routing trailing-slash ruby-on-rails-4

我已经尝试在application.rb中添加它

config.action_controller.default_url_options = { :trailing_slash => true }
Run Code Online (Sandbox Code Playgroud)

以及:trailing_slash => true在routes.rb中

match '/download', to: 'welcome#download', via: 'get', :trailing_slash => true
Run Code Online (Sandbox Code Playgroud)

但似乎都不起作用.我搜索了rails 4.0 doc但找不到相关信息.我在这里错过了什么?

更新:

我试过添加

Rails.application.default_url_options[:trailing_slash] = true
Run Code Online (Sandbox Code Playgroud)

filter_parameter_logging.rb因为这是在整个项目中,我唯一能找到的地方Rails.application.*,但它不工作要么.我在发布版本中找到了这一行,我使用的是4.0.4.我在错误的地方添加了这个吗?我在重新检查之前重新启动了服务器.

对于这个简单的问题感到抱歉,但是从我收集到的内容中也不trailing_slash应该反映在浏览器网址中,如果不是主要的话?因为这是我需要的,与历史相关.

com*_*tic 9

我认为你有:trailing_slash => true错误的意思.

它只是添加/到路径助手的末尾.没有涉及重定向.

无论有没有尾随斜线,您的路线仍会响应.

如果你想将所有非trailing_slashuri 重定向/download/download/使用nginx http服务器,你会做这样的事情:

rewrite ^([^.\?]*[^/])$ $1/ permanent;
Run Code Online (Sandbox Code Playgroud)

您仍然希望将:trailing_slash => true路径添加到路径中,以便路径/ url助手生成正确的uri(因此用户无需重定向).