Hide the Rails Authenticity Token

If you're using CSS styles in your Ruby on Rails forms, you may see a random box, especially if you put a background color on your input fields. Here's how to hide it: select the HTML element by name, instead of the more common id. A useful tip for new CSS desginers, too.

This was bugging me. A random grey box was showing up on our support form. After using Firebug to right click on it, and "Inspect Element", I could tell it was a hidden input form field, being inserted by Ruby on Rails. And it was showing up because we had a CSS style that made all form fields have a grey background. It's an authenticity token - meaning that the forms must have that to be submitted, to protect against CSRF attacks.

To remove it, I first stumbled on this lighthouse ticket, where someone has a patch to add a config option to Rails's environment. That might be useful for some, but I found it's also easy to select the element by it's name. This is a little different:


form#new_model div input[name="authenticity_token"] {
   display: none;
   background-color: #fff;
}

You can select any HTML element by an attribute. See section 5.1 in the CSS 2 specification.

Category:

Author: ivanoats

Created on: October 5th, 2009

Comments

Billee D.

over 2 years ago

That will nicely solve the task at hand, but if you want to catch all of those pesky hidden fields try this:


form input[type="hidden"] {
 display: none;
}
That one rule at the top of your style sheet will kill the display of all hidden fields. One rule to rule them all. ;-)

Ivan Storck

over 2 years ago

Nice Billee D! BTW, I forget how we're connected but I have seen your work before and love it!

leave a comment

Back to article list