# Scripts in RoR

**URL:** <https://www.ruby-forum.com/t/scripts-in-ror/142483>\
**Category:** Rails\
**Created:** [July 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/scripts-in-ror/142483 "2008-07-08T13:07:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jokrish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jokrish](https://www.ruby-forum.com/u/jokrish)\
**Post date:** [July 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/scripts-in-ror/142483/1 "2008-07-08T13:07:19Z")

</div>

i want to use scripts in my program.how can i write a script ? where  
it should be write in application.js and how.?and how can i call it  
from my view…can anybody help me with some simple examples.

---

<div class="post-metadata">

**Author:** ![jokrish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8edcca/32.png) [@jokrish](https://www.ruby-forum.com/u/jokrish)\
**Post date:** [July 8, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/scripts-in-ror/142483/2 "2008-07-08T15:13:12Z")

</div>

> i want to use scripts in my program.how can i write a script ? where  
> it should be write in application.js and how.?

app,ication.js is a good place for some general purpose scripts,  
that are used by all (or most) of your views  
don’t forget to use:  
\<%= javascript\_include\_tag :defaults %\>  
in the header section of your layout

but you can use your own js files as well, place them in public/  
javascripts  
and (assuming myscripts.js is the filename:  
\<%= javascript\_include\_tag ‘myscripts’ %\>

> and how can i call it  
> from my view…can anybody help me with some simple examples.

say you have defined a js function like  
function doSomething() {  
alert(‘hello world’);  
}

in a file & included it as described above, then it will be available  
as any js from your page. There are many ways to call this function  
for

one of the most simple:  
\<%= link\_to\_function(“hello”, “doSomething()”) %\>

will create a link and call this function.  
But you can use it as event handler in onclick, call it directly from  
any other function  
and so on.
