Ruby on rails 如何在Rails 4中不添加尾随的_斜杠到所有URL?
我已尝试将此添加到application.rb中Ruby on rails 如何在Rails 4中不添加尾随的_斜杠到所有URL?,ruby-on-rails,url,ruby-on-rails-4,url-routing,trailing-slash,Ruby On Rails,Url,Ruby On Rails 4,Url Routing,Trailing Slash,我已尝试将此添加到application.rb中 config.action_controller.default_url_options = { :trailing_slash => true } config.action_controller.default_url_options = { :trailing_slash => true } 以及routes.rb中的:training_slash=>true match '/download', to: 'welcome#
config.action_controller.default_url_options = { :trailing_slash => true }
config.action_controller.default_url_options = { :trailing_slash => true }
以及routes.rb中的:training_slash=>true
match '/download', to: 'welcome#download', via: 'get', :trailing_slash => true
但两者似乎都不起作用。我搜索了rails 4.0文档,但找不到相关信息。我错过了什么
更新:
我试着加上
Rails.application.default_url_options[:trailing_slash] = true
在filter\u parameter\u logging.rb
中,因为这是整个项目中唯一可以找到Rails.application.*
的地方,但它也不起作用。我找到了线路,我正在使用4.0.4。我是不是放错地方了?在重新检查之前,我确实重新启动了服务器
很抱歉问这么简单的问题,但从我收集的信息来看,如果不是主要的,浏览器url中不应该也反映出
尾随斜杠
?因为这就是我所需要的,来配合historyjs。我使用的是rails 4.0.2
,对我来说它很有效
routes.rb
match '/download', to: 'welcome#download', via: 'get', :trailing_slash => true
get 'admin/update_price_qty' => 'admin#update_price_qty', :trailing_slash => true,:as => "price"
match '/download', to: 'welcome#index', via: 'get', :trailing_slash => true,:as => "welcome_price"
在控制台中:-
irb(main):003:0* app.price_path
=> "/admin/update_price_qty/"
`irb(main):002:0> app.welcome_price_path
=> "/download/"`
routes.rb
match '/download', to: 'welcome#download', via: 'get', :trailing_slash => true
get 'admin/update_price_qty' => 'admin#update_price_qty', :trailing_slash => true,:as => "price"
match '/download', to: 'welcome#index', via: 'get', :trailing_slash => true,:as => "welcome_price"
在控制台中:-
irb(main):003:0* app.price_path
=> "/admin/update_price_qty/"
`irb(main):002:0> app.welcome_price_path
=> "/download/"`
但是我试着在application.rb中添加这个
config.action_controller.default_url_options = { :trailing_slash => true }
config.action_controller.default_url_options = { :trailing_slash => true }
不工作。尾随斜杠指的是名称后面的
/
,如page/
而不是/page
你把路线给错了
换成
match 'download/', to: 'welcome#download', via: 'get', :trailing_slash => true
还有其他方法可以实现这一点,方法是直接向您的link\u
助手提供trailing\u slash=>true
选项
link_to 'Downloads', downloads_path(:trailing_slash => true)
虽然这在Rails 3中起作用,但不确定Rails 4。
有关更多详细信息,请参见此。我认为您理解了
:training_slash=>true
error的含义
它所做的只是将/添加到路径帮助程序的末尾。不涉及重定向
您的路由仍将响应带有和不带尾部斜杠的路径
如果要使用nginx http服务器将所有非尾随斜杠
uri的/download
重定向到/download/
,可以执行以下操作:
rewrite ^([^.\?]*[^/])$ $1/ permanent;
您仍然需要将
:training_slash=>true
添加到路由中,以便您的路径/url帮助程序生成正确的uri(因此用户无需重定向)。您可以将此行添加到配置/application.rb
:
config.action_controller.default_url_options = { trailing_slash: true }
如果执行此操作,当您在控制器或帮助器中调用Rails路径帮助器时,生成的路径将在末尾有一个/
:
class ApplicationController
def index
download_path # returns "/download/"
end
end
module PathHelper
def path
download_path # returns "/download/"
end
end
如果需要在控制器和帮助程序之外使用路径帮助程序,则需要包括Rails.application.routes.url\u helpers
,但显然,这忽略了上面的尾部\u斜杠配置:
class SomeClass
include Rails.application.routes.url_helpers
def path
download_path # returns "/download"
end
end
在这种情况下,您应该添加{training_slash:true}
作为参数:
class SomeClass
include Rails.application.routes.url_helpers
def path
download_path(trailing_slash: true) # returns "/download/"
end
end
您是在谈论应用程序中生成的URL吗?@phoet是的,我该怎么做才能让所有
下载
自动重定向到下载
?我想您应该在web服务器级别这样做,您是在使用nginx吗?我必须在irb中看到结果吗?尾随的斜杠不也应该在浏览器地址栏中添加斜杠吗?这是我首先需要的,因为我需要使用historyjs来检测url更改。在Rails世界中,与PHP相反,所有的应用程序逻辑都保存在应用程序内部,而不是Apache或nginx配置。我认为这不是个好办法。