Error: "ActionController::InvalidAuthenticityToken"

Hi there,

I have my first Rails app running and I regularly get the following
"logged_exception" error message:


Has anybody an idea what might cause this problem? Could it somehow be a
"time out" error (like an "AuthenticityToken" which might expire after a
certain time, or something along those lines)?

Any idea how that error could be prevented from occurring?

The "backtrace" always starts like this:

The Authenticity Token is a value that is inserted in to forms (when using the form_for helper) that is then checked when the submit request is sent. It helps prevent CSRF attacks.

What is likely happening is that you’re generating your own form and not including the token (which you can do by inserting a hidden field and using the authenticity_token helper).

You can learn more about CSRF and Rails’ protection at:



Yeah, Andy is right. Why don’t you send us the code that generates these error requests? I have seen this error in some Ajax components like autocomplete. They create a form but do not send the token.

I post this into my blog:

It could also be caused by users with cookies disabled in the browser,
incorrect protect_from_forgery settings, or caching of authenticity

Thanks for your hints...

Well, the code is actually the following:

<%= check_box_tag 'applicationfile_verified',
      { :onclick => "#{remote_function(:url => {
                                          :controller =>
                                          :action => 'verify',
                                          :id => })}"
} %>

...which results in the following source...

<input class="confirm_testmail_checkbox" id="applicationfile_verified"
onclick="jQuery.ajax({data:'authenticity_token=' +
dataType:'script', type:'post', url:'/user/applicationfiles/1/verify'})"

And since the source includes...

    " data:'authenticity_token=' + encodeURIComponent('xV3Ayw...9Nnjs=')
" would mean the Authenticity Token is there and OK, right?

Seems fine to me!

Have you cached your view by any chance? That would mean that the
authenticity_token in the view is stored in a cached file and not really


Tom Ha wrote:

Does it always do it or only sometimes?

Only sometimes...

Maybe due to bots...?

We’ve had it happen on random occasions while using the RESTful authentication plugin after upgrading Rails. I remember reading a ticket issue somewhere about it and iirc it has to do with something funky in reset_session. But since we had the need for Rack-based authentication as well as some other features, we switched to Devise and have had no error reports ever since.

Best regards

Peter De Berdt

I can confirm that I use the RESTful authentication plugin, too.

Thanks for your input!