Rails 3.1资产——奇怪的开发服务

时间:2023-01-16 16:06:25

I've got a problem with Rails 3.1 assets pipeline. Assets are included twice in development:

我有一个关于Rails 3.1资产管道的问题。资产在开发中被包含两次:

<script src="/assets/main_new.js?body=1" type="text/javascript"></script>
<script src="/assets/pagenav.js?body=1" type="text/javascript"></script>
<script src="/assets/tours.controller.js?body=1" type="text/javascript"></script>
<script src="/assets/tours.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>

Rails somehow compiles and includes application.js so all the scripts are included twice - as individual file and in application.js

Rails以某种方式编译并包含应用程序。所有脚本都包含了两次,分别作为单独文件和application.js文件

Everything's fine with precompiled assets in production.

对生产中的预编译资产来说,一切都没问题。

development.rb

development.rb

 config.assets.compress = false
 config.assets.debug = true

production.rb

production.rb

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false

# Compress both stylesheets and JavaScripts
config.assets.compress = true
config.assets.js_compressor  = :uglifier
config.assets.css_compressor = :scss

config.assets.compile = false
config.assets.digest = true

application.rb

application.rb

config.assets.enabled = true

5 个解决方案

#1


39  

Try adding the following to development.rb:

试着在开发中添加以下内容。

config.serve_static_assets = false

...and then clearing your browser cache (update based on comments)

…然后清除浏览器缓存(根据注释更新)

The static assets refer to precompiled assets in public/assets, which is where rake assets:precompile puts them.

静态资产指公共/资产中的预编译资产,这就是rake资产:precompile将它们放在哪里。

What's happening is that anything that exists in public/assets will override anything in app/assets if you are serving them. So public/assets/application.js is being loaded when the js tag is intending to identifiy app/assets/application.js.

现在的情况是,公共/资产中存在的任何东西都将覆盖应用程序/资产中的任何东西,如果您正在服务它们的话。所以公共/资产/应用程序。当js标记要标识app/assets/application.js时,js正在被加载。

#2


12  

Once you get rid of /public/assets, you must also clear browser cache.

在删除/public/assets之后,还必须清除浏览器缓存。

#3


8  

This just caused me a problem. Setting the following makes the app work but includes the single application.js file - which I don't want in development:

这给我带来了麻烦。设置以下内容可以使应用程序工作,但包含单个应用程序。js文件-我不想在开发中使用:

config.serve_static_assets = false

I had pre-compiled my assets previously (seems to be the cause).

我之前已经预编译了我的资产(似乎是原因)。

To fix it I did the following:

为了修复它,我做了以下工作:

  • Remove the public/assets dir that the earlier precompilation had added.
  • 删除先前预编译添加的公共/资产目录。
  • Run RAILS_ENV=development rake assets:clean to clear out tmp/assets
  • 运行RAILS_ENV=开发rake资产:clean清除tmp/资产。
  • Edited app/assets/application.js
  • 编辑应用程序/资产/ application.js

It was only after I edited application.js so it errored and then corrected it that the applciation.js included in the pages was not the full, precompiled application.js.

只是在我编辑应用程序之后。所以它出错了,然后修正了这个问题。页面中包含的js不是完整的、预编译的application.js。

I'm not sure if all of those need to be done. I was also re-starting my server along the way.

我不确定是否所有这些都需要完成。我也重新启动了我的服务器。

#4


4  

Got tripped up by this (yet again), -- don't forget to add a BLANK LINE after all your //= require directives at the end of your application.js !

被这个错误绊倒了(又一次)——不要忘记在所有//=在应用程序末尾需要指令之后添加一个空行。js !

#5


0  

I add the same problem with less files.

我用更少的文件添加相同的问题。

Here from the documentation:

这里的文档:

In development mode, assets are served as separate files in the order they are specified in the manifest file.

在开发模式中,资产按照清单文件中指定的顺序作为单独的文件。

My solution was to remove the line *= require_tree . from application.css.less and to only use @import "my-styles"; from less.

我的解决方案是删除行*= require_tree。从application.css。较少且只使用@import“my-styles”;从更少。

Maybe you can find a similar solution with javascript...

也许你可以用javascript找到一个类似的解决方案…

#1


39  

Try adding the following to development.rb:

试着在开发中添加以下内容。

config.serve_static_assets = false

...and then clearing your browser cache (update based on comments)

…然后清除浏览器缓存(根据注释更新)

The static assets refer to precompiled assets in public/assets, which is where rake assets:precompile puts them.

静态资产指公共/资产中的预编译资产,这就是rake资产:precompile将它们放在哪里。

What's happening is that anything that exists in public/assets will override anything in app/assets if you are serving them. So public/assets/application.js is being loaded when the js tag is intending to identifiy app/assets/application.js.

现在的情况是,公共/资产中存在的任何东西都将覆盖应用程序/资产中的任何东西,如果您正在服务它们的话。所以公共/资产/应用程序。当js标记要标识app/assets/application.js时,js正在被加载。

#2


12  

Once you get rid of /public/assets, you must also clear browser cache.

在删除/public/assets之后,还必须清除浏览器缓存。

#3


8  

This just caused me a problem. Setting the following makes the app work but includes the single application.js file - which I don't want in development:

这给我带来了麻烦。设置以下内容可以使应用程序工作,但包含单个应用程序。js文件-我不想在开发中使用:

config.serve_static_assets = false

I had pre-compiled my assets previously (seems to be the cause).

我之前已经预编译了我的资产(似乎是原因)。

To fix it I did the following:

为了修复它,我做了以下工作:

  • Remove the public/assets dir that the earlier precompilation had added.
  • 删除先前预编译添加的公共/资产目录。
  • Run RAILS_ENV=development rake assets:clean to clear out tmp/assets
  • 运行RAILS_ENV=开发rake资产:clean清除tmp/资产。
  • Edited app/assets/application.js
  • 编辑应用程序/资产/ application.js

It was only after I edited application.js so it errored and then corrected it that the applciation.js included in the pages was not the full, precompiled application.js.

只是在我编辑应用程序之后。所以它出错了,然后修正了这个问题。页面中包含的js不是完整的、预编译的application.js。

I'm not sure if all of those need to be done. I was also re-starting my server along the way.

我不确定是否所有这些都需要完成。我也重新启动了我的服务器。

#4


4  

Got tripped up by this (yet again), -- don't forget to add a BLANK LINE after all your //= require directives at the end of your application.js !

被这个错误绊倒了(又一次)——不要忘记在所有//=在应用程序末尾需要指令之后添加一个空行。js !

#5


0  

I add the same problem with less files.

我用更少的文件添加相同的问题。

Here from the documentation:

这里的文档:

In development mode, assets are served as separate files in the order they are specified in the manifest file.

在开发模式中,资产按照清单文件中指定的顺序作为单独的文件。

My solution was to remove the line *= require_tree . from application.css.less and to only use @import "my-styles"; from less.

我的解决方案是删除行*= require_tree。从application.css。较少且只使用@import“my-styles”;从更少。

Maybe you can find a similar solution with javascript...

也许你可以用javascript找到一个类似的解决方案…