我正在尝试记录我的 REST API 应用程序的请求。我为此使用 Rails 通知,例如这里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时,它似乎不起作用!有人能告诉我为什么吗?谢谢