Carla Dessi
Carla Dessi

Reputation: 9666

Including a global variable at the top of a layout to use in all controller/views

I have an includes file with this content - @show = :dev I need to include this file in my layout so I can use this variable everywhere. I've tried including it:

.content
  = render "includes/dev_live"

but it only works if I reference it in every view rather than just once in the layout. I can't just type in the variable as my site is hosted on two servers each with different variables.

Upvotes: 0

Views: 130

Answers (1)

Slicedpan
Slicedpan

Reputation: 5015

Try the rails_config gem, it allows you to create settings files that are available in all controllers/views. For example have a settings file that reads:

:show
  'live'

then in your controllers/views you can access this by writing

Settings.show

(optionally converting to a symbol using to_sym if necessary) By default the rails config gem has a local settings file which is not tracked by version control (for git anyway), so you can have different versions on different instances.

Upvotes: 1

Related Questions