开发者

How to define a constant when running script/server?

开发者 https://www.devze.com 2022-12-25 16:46 出处:网络
I want to start up my Rails development server like this: script/server OFFLINE_MODE=1 and have a method in application_controller.rb that checks for the presence of that constant:

I want to start up my Rails development server like this:

script/server OFFLINE_MODE=1

and have a method in application_controller.rb that checks for the presence of that constant:

helper_method :offline_mode?
def offline_mode?
  defined?(OFFLINE_MODE) ? true : false
end

so I can hide stuff in my app when I'm coding without access to the internet. For开发者_运维技巧 some reason though, OFFLINE_MODE doesn't ever seem to be defined and the method always returns false.. thoughts?


You could use an environment variable to do this:

OFFLINE_MODE=1 script/server

def offline_mode?
    defined?(ENV['OFFLINE_MODE']) ? true : false
end


Try this:

script/server offline foo bar

Your helpers

helper_method :offline_mode?, :foo?
def offline_mode?
  ARGV.include?('offline')
end

# another example
def foo?
  ARGV.include?('foo')
end
0

精彩评论

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

关注公众号