简体   繁体   English

如何在Ruby on Rails中使用Javascript?

[英]How to use Javascript in Ruby on Rails?

I think I'm fundamentally misunderstanding how to implement Javascript in my rails app. 我认为我从根本上误解了如何在Rails应用程序中实现Javascript。 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. 我的理解是,将Javascript放入application.js文件,然后可以使用jQuery引用DOM中的元素以创建交互式网页。

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? 我的想法是我可能将Javascript放在错误的位置,或者我在某处缺少依赖项? I found other posts to be mostly unhelpful for me. 我发现其他帖子对我几乎没有帮助。 How do I merge Javascript into my app? 如何将Javascript合并到我的应用程序中?

application.js application.js

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

page.html.erb 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>

Have you found this? 找到了吗?
Where to Stick Your JavaScript . 在哪里粘贴JavaScript

Where to Stick Your JavaScript 在哪里粘贴您的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. 无论是使用Rails资产管道,还是直接在视图中添加<script>标记,都必须选择将本地JavaScript文件放置在何处。

We have a choice of three locations for a local JavaScript file: 对于本地JavaScript文件,我们可以选择三个位置:

  • the app/assets/javascripts folder app/assets/javascripts文件夹
  • the lib/assets/javascripts folder lib/assets/javascripts文件夹
  • the vendor/assets/javascripts folder vendor/assets/javascripts文件夹

Here are guidelines for selecting a location for your scripts: 以下是为脚本选择位置的准则

  • Use app/assets/javascripts for JavaScript you create for your application. 为您为应用程序创建的JavaScript使用app/assets/javascripts
  • Use lib/assets/javascripts for scripts that are shared by many applications (but use a gem if you can) . lib/assets/javascripts用于许多应用程序共享的脚本(但可以的话,请使用gem)
  • Use vendor/assets/javascripts for copies of jQuery plugins, etc., from other developers. 使用vendor/assets/javascripts从其他开发人员那里获取jQuery插件等的副本。

In the simplest case, when all your JavaScript files are in the app/assets/javascripts folder, there's nothing more you need to do. 最简单的情况下,当所有JavaScript文件都位于app/assets/javascripts文件夹中时,您无需执行任何其他操作。

Add JavaScript files anywhere else and you will need to understand how to modify a manifest file. 在其他任何地方添加JavaScript文件,您将需要了解如何修改清单文件。

Mysterious Manifests 神秘的清单
There are two kinds of files in a JavaScript assets folder: JavaScript资产文件夹中有两种文件:

  • ordinary JavaScript files 普通的JavaScript文件
  • manifest files 清单文件
    You can also have CoffeeScript files and ERB files which are variations on ordinary JavaScript files. 您还可以拥有CoffeeScript文件和ERB文件,它们是普通 JavaScript文件的变体。

Manifest files have the same .js file extension as ordinary JavaScript files. 清单文件具有与普通JavaScript文件相同的.js文件扩展名。 Manifest files and ordinary JavaScript files can be combined in a single file. 清单文件和普通JavaScript文件可以合并为一个文件。 This makes manifest files mysterious , or at least non-obvious. 这会使清单文件变得神秘 ,或者至少变得不明显。

The default app/assets/javascripts/application.js file is a manifest file . 默认的app / assets / javascripts / application.js文件是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. 指令告诉Sprockets应该合并哪些文件以构建单个JavaScript脚本。 Each file that contains manifest directives becomes a single JavaScript script with the same name as the original manifest file. 每个包含清单指令的文件都将成为一个与原始清单文件同名的JavaScript脚本。 Thus the app/assets/javascripts/application.js manifest file becomes the application.js script. 因此, app/assets/javascripts/application.js清单文件成为 application.js脚本。

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 . 当清单文件包含默认的// = require_tree时, app/assets/javascripts文件夹中的所有脚本都会自动添加到默认的application.js脚本中 directive. 指示。

Hope this helps. 希望这可以帮助。

If you're using Rails 5 you may need to add jQuery to your project by adding 如果您使用的是Rails 5,则可能需要通过添加jQuery将jQuery添加到您的项目中

gem 'jquery-rails'

in your Gemfile, as well as adding 在您的Gemfile中,以及添加

//= jquery

to your app > assets > javascript > application.js file. 到您的app > assets > javascript > application.js文件。

Since Rails 5 uses Turbolinks, you may also need to change the listener 由于Rails 5使用Turbolinks,因此您可能还需要更改侦听器

$(document).ready to $(document).on('turbolinks:load', function(main()); $(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. console.log添加到main功能将有助于调试以查看是否触发了该事件。

You forgot to add the parentheses after the function main() in document.ready 您忘记在document.ready中的main()函数之后添加括号。

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

In addition to this, try the following: 除此之外,请尝试以下操作:

Add the jquery-rails gem to your Gemfile (place this in your gem file): jquery-rails gem添加到您的Gemfile中(将其放置在您的gem文件中):

gem 'jquery-rails'

Then run bundle install 然后运行bundle install

Then write the following in your application.js : 然后在application.js编写以下内容:

//= require jquery 
//= require jquery_ujs 

The $(document).ready() expects a function, not the result of function call. $(document).ready()需要一个函数,而不是函数调用的结果。 Instead, you're calling main() before DOMReady event, and passing undefined into $(document).ready() function. 相反,您 DOMReady事件之前调用main() ,并将undefined传递给$(document).ready()函数。

It should be like this: 应该是这样的:

$(document).ready(main);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM