Rails 2.3.14:如何序列化 ActionController::Request 对象?

发布于 2024-12-07 04:33:03 字数 350 浏览 4 评论 0原文

我需要编写一些根据 Rails 2.3.14 控制器收到的请求对象类型执行操作的方法。但是,我不想启动整个应用程序,甚至不想启动控制器;我只想拥有这样一个对象的编组副本,以便我可以在 Rails 环境之外使用。

不幸的是,传递给控制器​​的 ActionController::Request 对象在其内部深处包含了本质上不可序列化的 Proc 对象。

有谁知道一种方法来序列化这些对象之一,以便我可以将其存储在数据文件中并在另一个脚本中重新创建它?我不想对 Proc 类进行猴子修补以提供 #marshal_dump 方法。

谢谢!

I need to write some methods that Do Things based on the kind of request object received by a Rails 2.3.14 controller. However, I don't want to fire up the entire application, nor even a controller; I'd like to have just a marshalled copy of such an object that I can work with outside of the Rails environment.

Unfortunately, the ActionController::Request objects passed to controllers include, deep in their bowels, Proc objects which are inherently unserialisable.

Does anyone know of a way to serialise one of these objects such that I can store it away in a data file and re-create it in another script? I would prefer not to monkey-patch the Proc class to provide a #marshal_dump method..

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2024-12-14 04:33:03

诀窍是替换/删除不可序列化的对象(尤其是在 env-hash 中)。我在这个SO答案中实现了其中的一部分。我使用 Rails 3.2.13 request 对象稍微更新了我的方法。

##
# build a serializable hash out of the given request object
def make_request_serializable(request)
  request_hash = {
    :env => request.env.clone,
    :filtered_parameters => request.filtered_parameters.clone,
    :fullpath => request.fullpath,
    :method => request.method,
    :request_method => request.request_method
  }
  #clean up the env-hash, as it contains not serializable objects
  request_hash[:env].tap do |env|
    env.delete "action_dispatch.routes"
    env.delete "action_dispatch.logger"
    env.delete "action_controller.instance"
    env.delete "rack.errors"
    env["action_dispatch.remote_ip"] = env["action_dispatch.remote_ip"].to_s 
  end
  request_hash
end

# later in the controller
puts make_request_serializable(request).to_json
#=> {"env":{"SERVER_SOFTWARE":"thin 1.5.1 codename Straight Razor","SERVER_NAME":"localhost","rack.input":[],"rack.version":[1,0], ... (shortened, as it's a lot of output)

更新:噢,我刚刚看到您在询问 Rails 2.3.14。我的视觉过滤器让我看到了 3.2.14。所以这仅适用于当前的 Rails 版本,抱歉。

The trick is to replace/delete not serializable objects (especially in the env-hash). I implemented parts of it in this SO answer. I slightly updated my approach using a rails 3.2.13 request object.

##
# build a serializable hash out of the given request object
def make_request_serializable(request)
  request_hash = {
    :env => request.env.clone,
    :filtered_parameters => request.filtered_parameters.clone,
    :fullpath => request.fullpath,
    :method => request.method,
    :request_method => request.request_method
  }
  #clean up the env-hash, as it contains not serializable objects
  request_hash[:env].tap do |env|
    env.delete "action_dispatch.routes"
    env.delete "action_dispatch.logger"
    env.delete "action_controller.instance"
    env.delete "rack.errors"
    env["action_dispatch.remote_ip"] = env["action_dispatch.remote_ip"].to_s 
  end
  request_hash
end

# later in the controller
puts make_request_serializable(request).to_json
#=> {"env":{"SERVER_SOFTWARE":"thin 1.5.1 codename Straight Razor","SERVER_NAME":"localhost","rack.input":[],"rack.version":[1,0], ... (shortened, as it's a lot of output)

Update: owww, I've just seen that you are asking vor Rails 2.3.14. My visual filter let me see 3.2.14 instead. So this only works for a current rails version, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文