Rails activesupport 通知 - 错误的数据库运行时值

2024-03-28

我正在尝试记录 REST API 应用程序的请求。我为此使用 Rails 通知,如下所示http://railscasts.com/episodes/249-notifications-in-rails-3 http://railscasts.com/episodes/249-notifications-in-rails-3

我不明白如何解决 Rails 通知的一个问题。

我的初始化代码

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
 p name
 p start 
 p finish
 p id
 p payload
end



Controller respond section

    class PostsController < ApplicationController
      # GET /posts
      # GET /posts.json

      respond_to  :json, :html
     ....
end

控制器创建动作

  def create
    @post = Post.new(params[:post])
    @post.save!
    respond_with(@post, :location => nil)
 end

控制台输出

"process_action.action_controller"
2013-02-02 20:13:11 +0200
2013-02-02 20:13:11 +0200
"951b8999e9b71d4a8949"
{:controller=>"PostsController", :action=>"create", :params=>{"utf8"=>"✓", "authenticity_token"=>"1WugY9gh6ZCRXjfBTuckye3c9XDvtCqMQ2JdBpCo88s=", "post"=>{"name"=>"post3", "title"=>"post3", "content"=>"post3"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"}, :format=>:html, :method=>"POST", :path=>"/posts", :status=>302, :view_runtime=>nil, :db_runtime=>0}

如你看到的:db_runtime=>0

但是,如果我将控制器操作代码更改为默认脚手架

  def create
    @post = Post.new(params[:post])
    #@post.save!
    #respond_with(@post)
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

我可以看到

"process_action.action_controller"
2013-02-02 20:22:51 +0200
2013-02-02 20:22:51 +0200
"bf2a3173c08a0fd9008e"
{:controller=>"PostsController", :action=>"create", :params=>{"utf8"=>"✓", "authenticity_token"=>"1WugY9gh6ZCRXjfBTuckye3c9XDvtCqMQ2JdBpCo88s=", "post"=>{"name"=>"post3", "title"=>"post3", "content"=>"post3"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"}, :format=>:html, :method=>"POST", :path=>"/posts", :status=>302, :view_runtime=>nil, :db_runtime=>4.727}

:db_runtime=>4.727

其原因是什么以及如何修复它以使其在第一个示例中工作? 谢谢 !

UPD

 bundle show rails
/Users/admin/.rvm/gems/ruby-1.9.3-p125/gems/rails-3.2.11
rvm current
ruby-1.9.3-p125

UPD2

当我使用respond_with时,它似乎不起作用!有人能告诉我为什么吗? 谢谢


好吧,这似乎是一个错误。让我们看看发生了什么:

首先,我们有用于控制器操作的 AR Railtie 及其使用 cleanup_view_runtime 挂钩设置 db_runtime 的实现

def cleanup_view_runtime
      if ActiveRecord::Base.connected?
       db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
       runtime = super
       db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
       self.db_runtime = db_rt_before_render + db_rt_after_render
       runtime - db_rt_after_render
     else
       super
     end
end

应用程序调用控制器操作 -> 操作执行一些数据库查询并渲染一些内容 -> 渲染之前和之后 AR Logger 保存运行时数据。好的。

让我们看看respond_with 是如何工作的

def respond_with(*resources, &block)
  raise "In order to use respond_with, first you need to declare the formats your " <<
        "controller responds to in the class level" if self.class.mimes_for_respond_to.empty?

  if collector = retrieve_collector_from_mimes(&block)
    options = resources.size == 1 ? {} : resources.extract_options!
    options[:default_response] = collector.response
    (options.delete(:responder) || self.class.responder).call(self, resources, options)
  end
end

def self.call(*args)
  new(*args).respond
end

def to_format
  if get? || !has_errors? || response_overridden?
    default_render
  else
    display_errors
  end
rescue ActionView::MissingTemplate => e
  api_behavior(e)
end

这里的代码似乎太多了,但是您应该看到此问题的调用堆栈:respond_with -> self.class.responder.respond -> self.class.responder.to_format -> default_render -> default_renderer raise ActionView::MissingTemplate(because我们没有)。此时我们可以看到通过捕获ActionView::MissingTemplate来渲染:json和:xml(api_behaviour)的实现。

现在我们知道respond_with是如何工作的,但AR Logger不知道.. cleanup_view_runtime钩子被调用两次:对于default_renderer(当时准备了模板数据并调用了一些数据库查询,但我们在渲染过程中捕获了ActionView::MissingTemplate)

db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
runtime = super # <-- here
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime

对于 api_behavour(当时所有模板数据都已准备好渲染并且没有数据库查询)

一些混乱的解释,但我希望它会有所帮助:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails activesupport 通知 - 错误的数据库运行时值 的相关文章

  • 定制导轨配置部分

    为 Rails 应用程序创建自定义配置部分的最佳方法是什么 理想情况下 我希望最终结果是一个 api 调用 例如 Rails configuration foo bar Rails configuration foo baz e g Rai
  • 在初始化程序中重新加载命名空间常量

    今天遇到一个有趣的情况 我不确定如何解决 给定一个带有初始化器的 Rails 应用程序 file config initializers integrations rb Integrations CONFIGS key gt value f
  • Rails 3 公司帐户具有许多用户,限制对数据的访问

    我想知道在我的应用程序中构建身份验证 授权的最佳方法 我希望有 许多公司帐户 可能使用子域 帐户有很多用户 用户只能访问自己或具有相同帐户的其他用户创建的记录 我所做的研究提供了许多混合搭配的想法 以奇怪而美妙的方式组合 devise ca
  • 测试驱动开发 - 我应该测试数据库列和索引吗?

    我是 TDD 新手 我发现shouldagem 能够测试数据库实体的列是否存在以及测试其索引 但是否有必要在我的测试套件中包含列和索引的测试 我是否需要担心在开发过程中可能会删除任何列和索引 不要测试数据库列 这只是测试实施 不要测试实现
  • Rails 资产 - 保留许可证注释

    如何防止 Uglifier 删除某些文件中的某些注释 我希望缩小和压缩代码 但我也希望许可注释保持不变 来自 uglifyJS 的文档 nc or no copyright 默认情况下 uglifyjs 将在生成的代码中保留初始评论标记 假
  • Rails 模型中的多个 counter_cache

    我正在学习 Rails 遇到了一个小问题 我正在编写带有任务列表的非常简单的应用程序 因此模型看起来像这样 class List lt ActiveRecord Base has many tasks has many undone tas
  • 使用 shoulda 重构 Rails 模型上的 rspec 测试

    了解后应该匹配器 https github com thoughtbot shoulda matchers通过回答关于属性可访问性测试的另一个 StackOverflow 问题 https stackoverflow com a 11849
  • blueprint/screen.css 未预编译

    我一直在遵循 Michael Hartl 出色的 RoR 教程 但我使用的是 RoR 3 1 我是 RoR 3 1 的新手 需要与资产管道相关的帮助 这是我的问题 在继续第 5 3 节之前 我想先转到 Heroku 看看事情如何发展 令我惊
  • Rails 3 - 如何完全避免数据库?

    我尝试在没有任何数据库后端的情况下使用 Rails 3 但当我尝试访问页面时它仍然坚持要求 sqlite3 gem 并抛出错误no such file to load sqlite3 尽管应用程序中没有代码需要 sqlite 但我将 dat
  • Rails_admin 中的范围作为过滤器

    我在我的应用程序中使用rails admin 我的模型上有一些范围 以下是一个示例 class User lt ActiveRecord Base scope unconfirmed where confirmed at IS NULL e
  • 如何使用 ActiveAdmin 创建 STI 子类的对象

    给出以下设置 当前不起作用 class Employee lt ActiveRecord Base end class Manager lt Employee end ActiveAdmin register Employee do for
  • 仅适用于安全页面的安全回形针 URL

    我正在尝试找到使回形针网址安全的最佳方法 但仅限于安全页面 例如 显示存储在 S3 中的图像的主页是http mydomain com http mydomain com图像网址是http s3 amazonaws com mydomain
  • 使用 prawnto_2 gem 加载图像时,RAILS_ROOT 不再有效

    我正在将我的应用程序从 Rails 3 0 升级到 Rails 3 1 我已经尽可能地将旧插件转为 gems 其中包括用这个漂亮闪亮的新插件替换旧的 prawnto 插件对虾 2 https github com forrest prawn
  • Rails 日志太详细

    如何防止 Rails 记录过多日志 这是我的 production log 文件中的典型跟踪 许多部分 缓存命中 它在开发中很有用 但我不希望在我的生产环境中使用它 Started GET redirected true for 46 19
  • Rails 的 Puma Systemd 配置不起作用

    我已经完成了一个使用 Ruby on Rails 构建的应用程序 现在我想将其托管在 AWS 上的 EC2 实例上 我已经为其配置了服务器 并且正在使用pumaHTTP服务器作为应用服务器 在生产中启动应用程序总是需要我运行RAILS EN
  • 如何对 mongodb/mongoid 脚本进行基准测试,以比较两种不同的查询技术

    您对如何测试两种不同的 mongoid mongodb 查询实现的性能有什么建议吗 要比较的实现与以前的相关 问答 https stackoverflow com questions 10121977 extracting modellin
  • 使用“回形针”gem 和 Ruby on Rails 3 时出现问题

    我在运行 Snow Leopard v1 6 5 的 MacO 上使用 Ruby on Rails 3 我想使用 回形针 gem 所以我将它包含在我的 Gemfile 中 如下所示 gem paperclip gt 2 3 然后 在终端中运
  • “array.map”是否保留原始顺序?

    我有一个User类has many Jobs 我使用以下代码映射作业 def ranges user jobs map u u start at u end at end 我有一个比较两个数组的规范 my array start1 end1
  • 无法使用 docker-compose 在 Dockerfile 中运行 rake db:create

    我有一个Dockerfile and docker compose yml就像在tutorial https docs docker com compose rails 除非我从现有的应用程序开始 My docker compose yml
  • 删除嵌套属性不起作用

    我似乎无法使用删除项目accepts nested attributes for命令 但我已经按照本教程 http railscasts com episodes 196 nested model form revised以及相关的git

随机推荐