views:

194

answers:

2

How can I read ENV variable

module MyModule
  def self.current_ip
    request.env['REMOTE_ADDR']
  end
end

MyModule::current_ip

How to?

A: 

why not just ENV['REMOTE_ADDR']?

Dave Sims
it is always nil.
xpepermint
+2  A: 

The problem here is that you are referencing the request object that doesn't exist in the module scope. You need to pass it or store it somewhere.

module MyModule
  mattr_accessor :request
  def self.current_ip
    request.env['REMOTE_ADDR']
  end
end

# store the request using a before filter
# or similar approach
MyModule.request = request

MyModule::current_ip

Depending on your case, there might be a more elegant solution.

Simone Carletti