Scripts in RoR

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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs