简体   繁体   English

在中间件中使用Rails.application.routes.recognize_path会破坏应用程序

[英]Using Rails.application.routes.recognize_path inside middleware breaks the app

The next middleware causes Rails app to fail loading assets 下一个中间件导致Rails应用无法加载资产

class Wtf
  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    # next line is causing all troubles
    Rails.application.routes.recognize_path request.path
    @app.call(env)
  end
end

if i replace the problem line with 如果我用替换问题行

Rails.application.routes.recognize_path '/'

then everything works again. 然后一切都会恢复。

How come that sending request.path as the argument to the recognize_path can cause app to be unable to load assets? 怎么说,发送request.path作为参数传递给recognize_path可能会导致应用无法装入资产?

The app can be found here https://github.com/mib32/wtf-middleware 该应用程序可以在这里找到https://github.com/mib32/wtf-middleware

The Rails asset pipeline compiles assets under the hashed paths you can see in the request, and those are handled differently than your other routing, so recognize_path won't behave properly. Rails资产管道在您在请求中可以看到的散列路径下编译资产,这些资产的处理方式与其他路由不同,因此recognize_path不能正常运行。 If you don't need your middleware to be messing with assets, you should skip these paths. 如果您不需要中间件来处理资产,则应跳过这些路径。

unless request.path =~ %r(^/assets/)
  Rails.application.routes.recognize_path request.path
end

Or, 要么,

begin
  Rails.application.routes.recognize_path request.path
rescue ActionController::RoutingError
  # pass
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails.application.routes.recognize_path无法识别POST路由 - Rails.application.routes.recognize_path does not recognize POST route 带有未定义方法身份验证的Rails.application.routes.recognize_path错误 - Rails.application.routes.recognize_path error with undefined method authenticate Rails.application.routes.recognize_path,设计经过验证的路由 - Rails.application.routes.recognize_path with devise authenticated route 在rspec测试中调用Rails.application.routes.recognize_path与Rails 3中的任何路由都不匹配 - Calling Rails.application.routes.recognize_path within an rspec test does not match any route in Rails 3 调用Rails.application.routes.generate()或Rails.application.routes.recognize_path()时,“ RuntimeError:路由集未完成” - 'RuntimeError: route set not finalized' when calling Rails.application.routes.generate() or Rails.application.routes.recognize_path() 通过recognize_path将请求URL参数与rails路由匹配 - Match request URL params with rails routes via recognize_path 访问rails中间件中的路由 - Access routes in rails middleware Rails 3,如何在路由中获取应用程序URL - Rails 3, How to get Application URL inside routes 使用request.env Inside Rails Middleware - Using request.env Inside Rails Middleware Ruby on Rails:没有使用edit_path的路线? - ruby on rails: no routes using edit_path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM