# Ajax calls in application.js (authenticity\_token problem?)

**URL:** <https://www.ruby-forum.com/t/ajax-calls-in-application-js-authenticity-token-problem/178267>\
**Category:** Rails\
**Created:** [November 1, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/ajax-calls-in-application-js-authenticity-token-problem/178267 "2009-11-01T17:15:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [November 1, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/ajax-calls-in-application-js-authenticity-token-problem/178267/1 "2009-11-01T17:15:44Z")

</div>

Hi,

I have a long list (~50) of items which should each be clickable and  
will send an Ajax request to the server. Now, each item will be calling  
the same action, just with a different id.

Ideally this looks like a clear case of putting the Ajax javascript in  
application.js. It’s in one place, just call a single function onclick  
and pass through the id.

However there are 2 issues:

1. I am worried that the Ajax request from application.js will not have  
access to any authenticity token as it was not generated by a Rails  
helper, and this could cause problems?

2. I’d prefer to use the Rails helpers to write the remote request if  
possible, in application.js I have to resort to hand-coding javascript.

The easy solution is just to have individual Ajax request code for each  
list item’s onclick call, but that would bulk out the file a fair bit  
and seems messy.

So I’m between a rock and a hard place…

Any advice much appreciated!

-P
