HTTP Request in Rails

Request is an object in ActionDispatch (ActionDispatch::Request < Object ), which in turn is part of ActionPack. It is available in Rails helper (which is available in View).

Controller is inherited from ActionController.

The request object is typically only accessible from the controller and the view. You could just pass the request to the methods that need it like any other object (dependency injection), but does your service object really need the whole request object? Why not just pass the subdomain string? In my opinion, service objects shouldn’t need to know about the request object as such behavior would probably be better handled from a controller method.

 

You can rapidly see the request.env in a view via:

  • VIEW: <%= request.env.inspect %>

If instead you want to log it in development log, from your controller:

  • CONTROLLER: Rails.logger.info(request.env)

Here you can see a reference for the Request object.

Rails automatically sets up logging to a file in the log/ directory using Logger from the Ruby Standard Library. The logfile will be named corresponding to your environment, e.g. log/development.log.

To log a message from either a controller or a model, access the Rails logger instance with the logger method:

class YourController < ActionController::Base
  def index
    logger.info request.env
  end
end