I need to log the time at which a request is made. How can I get that info from a request 开发者_C百科object in rails 3?
You need to create your Rack middleware, which will add a timestamp to request. For instance (haven't checked it, but it gives you the 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 in application.rb (config.middleware.use)
The request.env does not seem to include a timestamp:
<% for item in request.env %>
<%= item %><br />
<% end %>
And I don't see anything related here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html
Is it sufficient for your application to just set a timestamp in your controller in a before_filter?
精彩评论