How to add my JavaScript files to a Rails app?
I have a simple Rails scaffolded application, and would like to add a bunch of JavaScript files (including a special version of JQuery) to support new client-side features.
I added the *.js f开发者_JAVA百科iles to public/javascripts
and added <%= javascript_include_tag :all %>
at the top of the new.html.erb
where they are needed.
PROBLEM: When I first call $(document).ready(function(){
in this ERB, I get Uncaught TypeError: Object #<HTMLDocument> has no method 'ready'
, which seems to mean that my JQuery library has in fact not been loaded.
You probably want to be loading your javascript in the <head>
section of your HTML instead, to make sure it gets loaded at the proper time. When you put it in your new.html.erb
file, it's getting loaded in the body instead.
Find you layout, by default app/views/layouts/application.html.erb
, and put it in there in the <head>
section instead. This will then be applied to your new.html.erb
page.
Did you install the jquery-rails
gem by any chance ? Check this out.
精彩评论