Rails ENV variable inside Module
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']
?
精彩评论