最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to use Javascript in Ruby on Rails? - Stack Overflow

programmeradmin3浏览0评论

I think I'm fundamentally misunderstanding how to implement Javascript in my rails app. My understanding was that you throw your Javascript into your application.js file and then you can reference elements in your DOM using jQuery in order to make an interactive web page.

I just checked through all of my code, and it looks clean (posted below in case I'm wrong). My thought is that I may be putting my Javascript in the wrong place, or I'm missing a dependency somewhere? I found other posts to be mostly unhelpful for me. How do I merge Javascript into my app?

application.js

function main() {
  $('.answers-box').hide();
  $('.qaa-box').on('click', function() {
    $(this).next().slideToggle(400);
  });
}
$(document).ready(main());

page.html.erb

<div class="qaa-box">
  <h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit</h4>
  <p class="answers-box">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit
  </p>
</div>

I think I'm fundamentally misunderstanding how to implement Javascript in my rails app. My understanding was that you throw your Javascript into your application.js file and then you can reference elements in your DOM using jQuery in order to make an interactive web page.

I just checked through all of my code, and it looks clean (posted below in case I'm wrong). My thought is that I may be putting my Javascript in the wrong place, or I'm missing a dependency somewhere? I found other posts to be mostly unhelpful for me. How do I merge Javascript into my app?

application.js

function main() {
  $('.answers-box').hide();
  $('.qaa-box').on('click', function() {
    $(this).next().slideToggle(400);
  });
}
$(document).ready(main());

page.html.erb

<div class="qaa-box">
  <h4>Lorem ipsum dolor sit amet, consectetur adipiscing elit</h4>
  <p class="answers-box">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit
  </p>
</div>
Share Improve this question edited Oct 3, 2017 at 6:07 Styx 10.1k8 gold badges48 silver badges56 bronze badges asked Oct 2, 2017 at 22:41 MartinMartin 3051 gold badge2 silver badges10 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

Have you found this?
Where to Stick Your JavaScript.

Where to Stick Your JavaScript

Whether you use the Rails asset pipeline or add a <script> tag directly to a view, you have to make a choice about where to put any local JavaScript file.

We have a choice of three locations for a local JavaScript file:

  • the app/assets/javascripts folder
  • the lib/assets/javascripts folder
  • the vendor/assets/javascripts folder

Here are guidelines for selecting a location for your scripts:

  • Use app/assets/javascripts for JavaScript you create for your application.
  • Use lib/assets/javascripts for scripts that are shared by many applications (but use a gem if you can).
  • Use vendor/assets/javascripts for copies of jQuery plugins, etc., from other developers.

In the simplest case, when all your JavaScript files are in the app/assets/javascripts folder, there’s nothing more you need to do.

Add JavaScript files anywhere else and you will need to understand how to modify a manifest file.

Mysterious Manifests
There are two kinds of files in a JavaScript assets folder:

  • ordinary JavaScript files
  • manifest files
    You can also have CoffeeScript files and ERB files which are variations on ordinary JavaScript files.

Manifest files have the same .js file extension as ordinary JavaScript files. Manifest files and ordinary JavaScript files can be combined in a single file. This makes manifest files mysterious, or at least non-obvious.

The default app/assets/javascripts/application.js file is a manifest file. It’s a manifest file because it contains directives:

//= require jquery  
//= require jquery_ujs  
//= require_tree .

Directives tell Sprockets which files should be combined to build a single JavaScript script. Each file that contains manifest directives becomes a single JavaScript script with the same name as the original manifest file. Thus the app/assets/javascripts/application.js manifest file becomes the application.js script.

All scripts in the app/assets/javascripts folder are automatically added to the default application.js script when the manifest file includes the default //= require_tree . directive.

Hope this helps.

If you're using Rails 5 you may need to add jQuery to your project by adding

gem 'jquery-rails'

in your Gemfile, as well as adding

//= jquery

to your app > assets > javascript > application.js file.

Since Rails 5 uses Turbolinks, you may also need to change the listener

$(document).ready to $(document).on('turbolinks:load', function(main());

Adding a console.log to the main function will help with debugging to see if the event is being fired.

You forgot to add the parentheses after the function main() in document.ready

$(document).ready(main();)

In addition to this, try the following:

Add the jquery-rails gem to your Gemfile (place this in your gem file):

gem 'jquery-rails'

Then run bundle install

Then write the following in your application.js:

//= require jquery 
//= require jquery_ujs 

The $(document).ready() expects a function, not the result of function call. Instead, you're calling main() before DOMReady event, and passing undefined into $(document).ready() function.

It should be like this:

$(document).ready(main);
发布评论

评论列表(0)

  1. 暂无评论