开发者

Rails ENV variable inside Module

开发者 https://www.devze.com 2022-12-19 06:47 出处:网络
How can I read ENV variable module MyModule def self.current_ip request.env[\'REMOTE_ADD开发者_JAVA百科R\']

How can I read ENV variable

module MyModule
  def self.current_ip
    request.env['REMOTE_ADD开发者_JAVA百科R']
  end
end

MyModule::current_ip

How to?


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.


why not just ENV['REMOTE_ADDR']?

0

精彩评论

暂无评论...
验证码 换一张
取 消