[英]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.