xpepermint
xpepermint

Reputation: 36273

Rails ENV variable inside Module

How can I read ENV variable

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

MyModule::current_ip

How to?

Upvotes: 0

Views: 1388

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176552

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.

Upvotes: 2

Dave Sims
Dave Sims

Reputation: 5128

why not just ENV['REMOTE_ADDR']?

Upvotes: 0

Related Questions