开发者

How to define a constant when running script/server?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜