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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)