简体   繁体   English

Rails 3.1 Asset Pipeline中的Javascript文件

[英]Javascript files in Rails 3.1 Asset Pipeline

I've tested my Rails app's functionality by placing jQuery at the bottom of my home.html.erb file with simple script tags. 我已经通过使用简单的script标记将jQuery放在home.html.erb文件的底部来测试了Rails应用程序的功能。 Everything works fine until I attempt to utilize the Asset Pipleine in Rails 3.1 and place the script within app/javascripts/home.js.erb 一切正常,直到我尝试在Rails 3.1中利用Asset Pipleine并将脚本放在app/javascripts/home.js.erb

Anyone know why I can't get the javascript to work outside of the home.html.erb file 任何人都知道为什么我无法让javascript在home.html.erb文件之外运行

you could add the script tag in the application.html.erb and you'll have jquery in all your pages. 您可以在application.html.erb中添加脚本标签,然后所有页面中都将包含jquery。 you can add the script in this way: (you should add the jquery-1.7.min file in the folder app/javascript) 您可以通过以下方式添加脚本:(您应该在app / javascript文件夹中添加jquery-1.7.min文件)

    <%= javascript_include_tag 'jquery-1.7.min'%> 

Don't put home.js.erb in app/javascripts but extract your jquery from home.js.erb and put in app/assets/javascripts/home.js 不要将home.js.erb放入app / javascript中,而是从home.js.erb中提取您的jquery并放入app / assets / javascripts / home.js中

add config.assets.precompile += %w( *.js ) to config/environments/production.rb or in config/application.rb for global env (test/dev/prod) config.assets.precompile + =%w(* .js)添加config / environments / production.rb或在config / application.rb中添加全局环境(test / dev / prod)

than in app/assets/javascripts/application.js have app / assets / javascripts / application.js中的

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

call it with javascript_include_tag "application" from the view 从视图中使用javascript_include_tag“ application”进行调用

... and finally, don't forget to add gem 'jquery-rails' to your Gemfile and run bundle ...最后,不要忘了将gem'jquery -rails'添加到您的Gemfile并运行

As far as what I was looking to do, which was separate javascript files for different views, I was unsuccessful. 就我想做的是针对不同视图的单独javascript文件而言,我没有成功。 I tried different naming conventions, etc. 我尝试了不同的命名约定,等等。

What has worked for me however, is putting all of my javascript within application.js . 但是对我有用的是,将我所有的javascript都放在application.js Doing this makes this code accessible by every view throughout the entire application. 这样做使整个应用程序中的每个视图都可以访问此代码。 Depending upon your needs and your app, you may want to put the script within [your-model-name].js.coffee which will make it accessible (I think just for your particular controllers and views). 根据您的需求和您的应用程序,您可能希望将该脚本放入[your-model-name].js.coffee ,以使其可访问(我认为仅适用于您的特定控制器和视图)。 Since my app is a single model/controller setup, this is essentially the same thing. 由于我的应用是单个模型/控制器设置,因此本质上是相同的。

Then, it's a matter of using selectors carefully as to not interfere with other pages unintentionally. 然后,要谨慎使用选择器,以免无意间干扰其他页面。 In other words, if you want a piece of code to run within one particular view and not another, you will have to adjust your .js code accordingly, since everything within application.js is accessible to all views. 换句话说,如果您希望一段代码在一个特定视图中运行而不在另一个特定视图中运行,则由于所有视图都可以访问application.js中的所有内容,因此您将必须相应地调整.js代码。

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

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