简体   繁体   English

使用单个脚本文件和Rails资产

[英]Using individual script files and rails assets

My app uses the old version of jquery, and I wanted to do a POC inside my app using the new jquery version to show some of the benefits and at the same time rewrite some of the back-end. 我的应用程序使用旧版本的jquery,我想使用新的jquery版本在我的应用程序内部进行POC,以显示一些好处,同时重写一些后端。

So what I did was that I added a layout to my controller named poc , and I removed the application javascript tag from the poc layout. 因此,我要做的是将布局添加到名为poc控制器中,然后从poc布局中删除了应用程序javascript标签。 But I left the this inside poc layout: 但我在this poc布局中保留了以下内容:

= yield :footer_js

So that I can load javascript on the individual page, which I did like this : 这样我就可以在单个页面上加载javascript,就像这样:

- content_for :footer_js do
  = javascript_include_tag 'new_jquery'

And on my new page only jquery loads up, which I added to app/assets/javascript/poc/new_jquery.js and it all works perfectly well on my local machine (development environment). 并且在我的新页面上,仅加载了jquery,我将其添加到app/assets/javascript/poc/new_jquery.js ,并且在我的本地计算机(开发环境)上都运行良好。

However once I deploy it to production it's a different story, nothing is loading up. 但是,一旦我将其部署到生产中,情况就大不一样了,什么也没装。 My scripts are located at app/assets/javascript/poc and under this there is new_jquery.js itself. 我的脚本位于app/assets/javascript/poc之下,在其下有new_jquery.js本身。

And then there are files under the app/assets/javascript/poc/services and app/assets/javascript/validations which I individually load on view. 然后在app/assets/javascript/poc/servicesapp/assets/javascript/validations下有一些文件,我分别在视图中加载。

Question: Am I doing this the right way? 问题:我这样做正确吗? And if yes, how do I get these file to work in production with assets pipeline? 如果是的话,如何使这些文件在资产管道中投入生产? Or if I'm doing it the wrong way, what's the right way to test an isolated page with isolated dependencies in production? 或者,如果我用错误的方式进行操作,那么在生产环境中测试具有独立依赖关系的隔离页面的正确方法是什么?

What I did was as described in the answer I end up using the new layout, however instead of individually including the javascript files on the view, I added a poc.js in the app/assets/javascript next to the application.js , and referenced all the js files I needed from poc.js and included poc.js in the layout. 我所做的是如答案中所述,我最终使用了新的布局,但是我没有在视图中单独包含javascript文件, poc.jsapplication.js旁边的app/assets/javascript添加了poc.js ,引用的所有js文件,我从需要poc.js并列入poc.js布局。

Also I added poc.js in the application.rb : 另外我在application.rb中添加了poc.js

Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'poc.js']

Worked great! 很棒!

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

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