How to get the time when a request is made from a request object in rails 3?

I need to record the time at which the request was made. How can I get this information from a request object in rails 3?

+5
source share
2 answers

You need to create a Rack middleware that will add a timestamp for the request. For example (not tested, but this gives you an idea):


module Middleware
  class Timestamp
    def initialize(app)
      @app = app
    end

    def call(env)
      env[:timestamp] = Time.now
      @app.call(env)
    end
  end
end

And add it to application.rb (config.middleware.use)

+10
source

Request.env is missing a timestamp:

<% for item in request.env %>
  <%= item %><br />
<% end %>

And I don’t see anything like it here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html

before_filter?

+1

All Articles