在 Rails 中,我可以在 action 返回之前访问 response.body 吗? [英] In rails, can I access response.body in a action before it returns?
本文介绍了在 Rails 中,我可以在 action 返回之前访问 response.body 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Rails 中,我可以在 action 返回之前访问 response.body 吗?
In rails, can I access response.body in a action before it returns?
假设我想在它返回之前做一些最终的字符串替换,我可以访问 response.body,即视图返回的响应吗?
Say I want to do some final string replacements before it returns, can I get access to response.body i.e. the response that the view returns?
推荐答案
您可以编写机架中间件来执行此类替换.机架的代码是.
You can write a rack middleware to do such kind of replacements. Code for the rack is.
module Dump
require 'rack'
class Response
def initialize(app)
@app=app
end
def call(env)
res=@app.call(env)
res.body #change this and but also update res.length and header["Content-Length"]
return res
end
end
end
将它包含在某个文件中,让我们在 RAILS_ROOT/lib 文件夹中将其命名为 dump_response.rb.和线
include it in some file, lets call it dump_response.rb in RAILS_ROOT/lib folder. And line
use Dump::Response
在 config.ru
in config.ru
这篇关于在 Rails 中,我可以在 action 返回之前访问 response.body 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文