I'm fairly new to Ruby on Rails and I'm planing to use a fair amount of javascript in my application. I have done some research but I couldn't find an answer to this one question:
Since you're able to put simple html in all of the views (for example index.html.erb) I was under the assumption that the views basically act as a normal html webpage and therefore you should be able to do everything you can do with a normal html webpage. For example put javascript in it from any library you like as long as you include the right doctypes and provide tags for the sources to the libraries as you would in any other html webpage. Is this correct or is there a catch I'm overlooking?
Because I found a lot on the internet about how it's not so simple to use RoR with any javascript library and that you have to take special steps and so forth...
I really would appreciate it if someone could shed some light on this for me. Thanks!
Just point the source place of your javascript files at your layout/application.html.erb file.
e.g: <%= javascript_include_tag :defaults %>.
This will load all the javascript files that are present in your public/javascripts/ directory.
If you need to be more specific you could also put each file separated by a simple comma, like this:
<%= javascript_include_tag 'jquery.js', 'application.js', 'jquery_ujs.js','jquery-ui-1.8.15.custom.min.js' %>
And it's a better approach to put all your javascript code on a file inside that directory.
If it's for general usage you could put the code in the public/javascripts/application.jsfile.
Hope it helps you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With