2

Following the answer on How to define custom configuration variables in rails, I am trying to set up a configuration settings for different environments in config/environments/{env}.rb

e.g. in development.rb I set

config.elvis = 'alive'

and then in my haml template I can use this variable, e.g.

Elvis is #{Rails.configuration.elvis}.

However, when I want to wrap this in a condition:

- if Rails.configuration.elvis
  <p>Elvis is #{Rails.configuration.elvis}</p>

it also works, but if the configuration isn't set, it throws a undefined method error.

If I try instead:

- if defined? Rails.configuration.elvis
  <p>Elvis is #{Rails.configuration.elvis}</p>

it seems to always evaluate as false, even with the configuration defined.

Still very new to rails/ruby, so apologies if it's a very dumb question

Community
  • 1
  • 1
gingerlime
  • 5,206
  • 4
  • 37
  • 66

1 Answers1

3

You could use respond_to?:

- if Rails.configuration.respond_to?(:elvis)
  <p>Elvis is #{Rails.configuration.elvis}</p>
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • Thanks for the super-fast response! Worked like a charm (will mark as answered in 7 minutes or so when I'm allowed...) – gingerlime Apr 13 '12 at 10:39