# Rails syntax in application.js

**URL:** <https://www.ruby-forum.com/t/rails-syntax-in-application-js/137650>\
**Category:** Rails\
**Created:** [May 15, 2008, 12:36am UTC](https://www.ruby-forum.com/t/rails-syntax-in-application-js/137650 "2008-05-15T00:36:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [May 15, 2008, 12:36am UTC](https://www.ruby-forum.com/t/rails-syntax-in-application-js/137650/1 "2008-05-15T00:36:16Z")

</div>

Is it possible to use the rails syntax inside of application.js file.  
Basically, I want to use remote\_function call inside of a javascript  
function.  
I know I could use the Ajax.Request but then I have a problem with an  
authenticity token.

I’ve tried this:  
function callRemote(){  
\<%= remote\_function(:url =\> {:action =\> :my\_function}) %\>  
}  
And this syntax does not fly in the application.js.  
So is it possible to use the rails syntax in some way in application.js  
file?

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [May 15, 2008, 1:27am UTC](https://www.ruby-forum.com/t/rails-syntax-in-application-js/137650/2 "2008-05-15T01:27:52Z")

</div>

No. This is just a javascript file, you cannot use Rails syntax in it.

---

<div class="post-metadata">

**Author:** ![russianbandit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@russianbandit](https://www.ruby-forum.com/u/russianbandit)\
**Post date:** [May 15, 2008, 2:40am UTC](https://www.ruby-forum.com/t/rails-syntax-in-application-js/137650/3 "2008-05-15T02:40:18Z")

</div>

Greg L. wrote:

> Is it possible to use the rails syntax inside of application.js file.  
> Basically, I want to use remote\_function call inside of a javascript  
> function.  
> I know I could use the Ajax.Request but then I have a problem with an  
> authenticity token.
> 
> I’ve tried this:  
> function callRemote(){  
> \<%= remote\_function(:url =\> {:action =\> :my\_function}) %\>  
> }  
> And this syntax does not fly in the application.js.  
> So is it possible to use the rails syntax in some way in application.js  
> file?

ruby script/generate controller Javascripts

add “map.connect ‘javascripts/:action.js’” to config/routes.rb

create app/views/javascripts/app.erb

add the line “caches\_page :app” to  
app/controllers/javascripts\_controller.rb

in your template add “javascript\_include\_tag(:app)”

* * *

now you have app.js  
dynamically generated from erb  
and cached.
