How to include the JavaScript source in RoR

Hi guys,

I am a newbie in developing in Ruby on Rails. I don't understand how to include the a javascript file. so like i.e. in general HTML I use this <script type="text/javascript" src="js/xxx.js"></script> it seems like I couldn't do this directly, so I need to put the javascript file in the script folder. so how do direct to the file? also, how about a href and img src?

Thanks in Advance, Richard

Hi guys,

I am a newbie in developing in Ruby on Rails. I don't understand how to include the a javascript file. so like i.e. in general HTML I use this <script type="text/javascript" src="js/xxx.js"></script>

You can certainly do it that way if you want to (after all, by the time it gets to the browser it is just html so anything you could do without rails you can do with rails). What error did you get when you tried it? As you have coded it the file would be expected in public/js. More conventionally they would go in public/javascripts but that is up to you.

More conventionally in rails you would use javascript_include_tag to include them, it saves typing and avoids having to remember the html format.

it seems like I couldn't do this directly, so I need to put the javascript file in the script folder. so how do direct to the file? also, how about a href and img src?

Look at link_to and image_tag

Perhaps also work through some tutorials that will introduce you to all these things. railstutorial.org is good and is free to use online. Make sure that any tutorial you use matches the rails version you are using (the first two digits anyway, eg 3.0)

Colin

Colin