在Rails中显示页面加载时间3

Mar*_*man 9 ruby-on-rails actionview ruby-on-rails-3

如何在视图中显示页面加载时间,类似于日志文件显示"在19ms内完成200 OK(视图:16.8ms |模型:0.497ms)"

小智 15

您可能希望更好地使用Seconds:

class ApplicationController < ActionController::Base

  before_filter :set_start_time

  def set_start_time
    @start_time = Time.now.to_f
  end

end
Run Code Online (Sandbox Code Playgroud)

查看代码:

Page Rendered in <%= sprintf('%.3f', (Time.now.to_f - @start_time) ) %> seconds
Run Code Online (Sandbox Code Playgroud)


joh*_*ley 10

你可以这样做..在你的application_controller中添加一个before_filter:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :init

  def init
    @start_time = Time.now
  end
end
Run Code Online (Sandbox Code Playgroud)

在视图中(我正在使用HAML):

load_time=#{Time.now-@start_time} seconds
Run Code Online (Sandbox Code Playgroud)

这与您在日志中看到的时间不完全相同,因为它只是从before_filter到它在视图中调用的地方,但它应该是接近的.