# Replytotopic Random Issue with Invalid AuthenticityToken

**URL:** <https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630>\
**Category:** Rails\
**Created:** [June 24, 2010, 9:34am UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630 "2010-06-24T09:34:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [June 24, 2010, 9:34am UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630/1 "2010-06-24T09:34:47Z")

</div>

Greetings

I would appreciate any thoughts or ideas on the following issue:

I have a RoR application with all forms created dynamically.  
Unfortunately some times when you hit the back button of the browser and  
try to login via the Login form an error message will come with â€œInvalid  
Authenticity Tokenâ€.

Can anyone suggest what is wrong or had any similar experience before ?

Thank you!

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [June 24, 2010, 10:53am UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630/2 "2010-06-24T10:53:30Z")

</div>

John M. wrote:

> Greetings
> 
> I would appreciate any thoughts or ideas on the following issue:
> 
> I have a RoR application with all forms created dynamically.  
> Unfortunately some times when you hit the back button of the browser and  
> try to login via the Login form an error message will come with â€œInvalid  
> Authenticity Tokenâ€.
> 
> Can anyone suggest what is wrong or had any similar experience before ?
> 
> Thank you!

Sample output from form\_tag:

 Form contents 

If you carefully observe this output, you can see that the helper  
generated something you didnâ€™t specify: a div element with a hidden  
input inside. This is a security feature of Rails called cross-site  
request forgery protection and form helpers generate it for every form  
whose action is not â€œgetâ€ (provided that this security feature is  
enabled). You can read more about this in the Ruby On Rails Security  
Guide.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [June 24, 2010, 11:22am UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630/3 "2010-06-24T11:22:38Z")

</div>

Bohdan P. wrote:

> John M. wrote:
> 
> > Greetings
> > 
> > I would appreciate any thoughts or ideas on the following issue:
> > 
> > I have a RoR application with all forms created dynamically.  
> > Unfortunately some times when you hit the back button of the browser and  
> > try to login via the Login form an error message will come with â€œInvalid  
> > Authenticity Tokenâ€.
> > 
> > Can anyone suggest what is wrong or had any similar experience before ?
> > 
> > Thank you!
> 
> Sample output from form\_tag:
> 
> Form contents 
> 
> If you carefully observe this output, you can see that the helper  
> generated something you didnâ€™t specify: a div element with a hidden  
> input inside. This is a security feature of Rails called cross-site  
> request forgery protection and form helpers generate it for every form  
> whose action is not â€œgetâ€ (provided that this security feature is  
> enabled). You can read more about this in the Ruby On Rails Security  
> Guide.

Thank you for your feedback Bohdan.  
I have checked my code and it produces the hidden div correctly. The  
problem is that some times it works perfectly but then some others  
(rarely) it produces the Invalid Token Authenticity.

The problem is that I cannot reproduce the error so I cannot figure out  
what the problem is.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [July 4, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630/4 "2010-07-04T16:19:54Z")

</div>

John M. wrote:

> Bohdan P. wrote:
> 
> > John M. wrote:
> > 
> > > Greetings
> > > 
> > > I would appreciate any thoughts or ideas on the following issue:
> > > 
> > > I have a RoR application with all forms created dynamically.  
> > > Unfortunately some times when you hit the back button of the browser and  
> > > try to login via the Login form an error message will come with â€œInvalid  
> > > Authenticity Tokenâ€.
> > > 
> > > Can anyone suggest what is wrong or had any similar experience before ?
> > > 
> > > Thank you!
> > 
> > Sample output from form\_tag:
> > 
> > Form contents 
> > 
> > If you carefully observe this output, you can see that the helper  
> > generated something you didnâ€™t specify: a div element with a hidden  
> > input inside. This is a security feature of Rails called cross-site  
> > request forgery protection and form helpers generate it for every form  
> > whose action is not â€œgetâ€ (provided that this security feature is  
> > enabled). You can read more about this in the Ruby On Rails Security  
> > Guide.
> 
> Thank you for your feedback Bohdan.  
> I have checked my code and it produces the hidden div correctly. The  
> problem is that some times it works perfectly but then some others  
> (rarely) it produces the Invalid Token Authenticity.
> 
> The problem is that I cannot reproduce the error so I cannot figure out  
> what the problem is.

Greetings

The problem arises when:

1. I logout from the app and go to login form
2. then visit another page (clicking on a link)
3. hit the Back button to return to the login form
4. try to login

I get also this message too :

* * *

## The change you wanted was rejected. Maybe you tried to change something you didn’t have access to.

Here is the code for the authenticity token:

Hope that helps

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [July 4, 2010, 6:59pm UTC](https://www.ruby-forum.com/t/replytotopic-random-issue-with-invalid-authenticitytoken/190630/5 "2010-07-04T18:59:33Z")

</div>

On Jul 4, 3:19 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Sample output from form\_tag:
> 
> > > Form contents

The authenticity token is based upon some data stored in the session:  
if when you logout you reset the session (which very sensibly most  
people do) and you reset the session after the form is rendered then  
the form contains a no longer valid authenticity token. When you press  
the back button this page is fetched from the cache and so you submit  
a form with that stale token

Fred
