Read more

Detect the current Rails environment from JavaScript or CSS

Henning Koch
August 04, 2011Software engineer at makandra GmbH

Detecting if a Javascript is running under Selenium WebDriver is super-painful Show archive.org snapshot . It's much easier to detect the current Rails environment instead.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

You might be better of checking against the name of the current Rails environment. To do this, store the environment name in a data-environment of your <html>. E.g., in your application layout:

<html data-environment=<%= Rails.env %>>

Now you can say in a piece of Javascript:

if (document.documentElement.dataset.environment == 'test') {
  // Code that should happen in Selenium tests
} else {
  // Code that should happen for other environments
}

Or in your CSS / Sass:

html[data-environment="test"] {
  * { text-transform: none !important; }
}

See also

Posted by Henning Koch to makandra dev (2011-08-04 12:32)