Rails 4:在生产中不加载的资产。

时间:2023-01-16 16:01:26

I'm trying to put my app into production and image and css asset paths aren't working.

我试着把我的应用程序投入到生产和图像中,而css的资产路径却行不通。

Here's what I'm currently doing:

以下是我目前正在做的事情:

  • Image assets live in /app/assets/images/image.jpg
  • 图像资产生活在/应用/资产/图像/图像。jpg。
  • Stylesheets live in /app/assets/stylesheets/style.css
  • 样式表活在/ app /资产/ / style.css样式表
  • In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
  • 在我的布局中,我引用了这样的css文件:<%=样式表- link_tag "style ", media: "all", "data-turbolinks-track" => true %>。
  • Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompile and it succeeds and I see the fingerprinted files in the public/assets directory.
  • 在重新启动独角兽之前,我运行RAILS_ENV=生产bundle exec rake资产:预编译和它成功,我在公共/资产目录中看到了指印文件。

When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.

当我浏览到我的网站时,我得到了一个404页面,没有发现mysite.com/样式表/styles.css的错误。

What am I doing wrong?

我做错了什么?

Update: In my layout, it looks like this:

更新:在我的布局中,它看起来是这样的:

<%= stylesheet_link_tag    "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag    "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

The generate source is this:

生成源是这样的:

<link data-turbolinks-track="true" href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>

Looks like Rails is not properly looking for the compiled css files. But it's very confusing why it's working correctly for javascripts (notice the /assets/****.js path).

看起来Rails没有正确地寻找已编译的css文件。但是,为什么它能正确地为javascript工作(请注意/assets/****),这很让人困惑。js路径)。

17 个解决方案

#1


97  

In rails 4 you need to make the changes below:

在rails 4中,您需要进行以下更改:

config.assets.compile = true
config.assets.precompile =  ['*.js', '*.css', '*.css.erb'] 

This works with me. use following command to pre-compile assets

这与我。使用以下命令来预编译资产。

RAILS_ENV=production bundle exec rake assets:precompile

Best of luck!

最好的运气!

#2


70  

I just had the same problem and found this setting in config/environments/production.rb:

我刚刚遇到了同样的问题,并在config/environment /production.rb中找到了这个设置。

# Rails 4:
config.serve_static_assets = false

# Or for Rails 5:
config.public_file_server.enabled = false

Changing it to true got it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?

把它改成true就可以了。默认情况下,Rails希望您将前端webserver配置为处理来自公用文件夹的文件请求,而不是将其代理到Rails应用程序。

(See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILES environment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.

(见5 Rails文档)。正如在注释中所指出的,在Rails 5中,您可以设置RAILS_SERVE_STATIC_FILES环境变量,因为默认设置是config.public_file_server。启用= ENV[' RAILS_SERVE_STATIC_FILES '].present ?。

#3


31  

In /config/environments/production.rb I had to add this:

在/ config /环境/生产。我必须加上这个:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

js已经预编译了,但是我还是添加了。. css和. css。erb显然不是自动发生的。^(^ _)排除——这是一个正则表达式编译泛音。

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

有一点让人沮丧的是,文档清楚地指出,资产管道是默认启用的,但并没有阐明只适用于javascript的事实。

#4


18  

I was able to solve this problem by changing: config.assets.compile = false to
config.assets.compile = true in /config/environments/production.rb

我可以通过改变:config.assets.compile = false到config.assets.compile = true in /config/environment /production.rb来解决这个问题。

#5


9  

For Rails 5, you should enable the follow config code:

对于Rails 5,您应该启用以下配置代码:

config.public_file_server.enabled = true

config.public_file_server。启用= true

By default, Rails 5 ships with this line of config:

默认情况下,Rails 5使用这一行配置:

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

config.public_file_server。启用= ENV[' RAILS_SERVE_STATIC_FILES '].present吗?

Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILES to true.

因此,您需要将环境变量RAILS_SERVE_STATIC_FILES设置为true。

#6


8  

There are 2 things you must accomplish to serve the assets in production:

在生产过程中,你必须完成两件事:

  1. Precompile the assets.
  2. 预编译的资产。
  3. Serve the assets on the server to browser.
  4. 在服务器上服务于浏览器。

1) In order to precompile the assets, you have several choices.

为了预编译资产,你有几个选择。

  • You can run rake assets:precompile on your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.

    您可以运行rake资产:在本地机器上预编译,提交给源代码控制(git),然后运行部署程序,例如capistrano。这不是将预编译的资产提交SCM的好方法。

  • You can write a rake task that run RAILS_ENV=production rake assets:precompile on the target servers each time you deploy your Rails app to production, before you restart the server.

    您可以编写一个rake任务,运行RAILS_ENV=生产rake资产:每次您将Rails应用程序部署到生产中,在重新启动服务器之前,在目标服务器上进行预编译。

Code in a task for capistrano will look similar to this:

capistrano任务中的代码将与此类似:

on roles(:app) do
  if DEPLOY_ENV == 'production'
    execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
  end
end

2) Now, you have the assets on production servers, you need to serve them to browser.

现在,在生产服务器上有了这些资产,您需要将它们提供给浏览器。

Again, you have several choices.

同样,你有几个选择。

  • Turn on Rails static file serving in config/environments/production.rb

    打开在config/environment /production.rb中服务的Rails静态文件。

    config.serve_static_assets = true # old
    
    or
    
    config.serve_static_files = true # new
    

    Using Rails to serve static files will kill your Rails app performance.

    使用Rails来服务静态文件会破坏Rails应用程序的性能。

  • Configure nginx (or Apache) to serve static files.

    配置nginx(或Apache)来服务静态文件。

    For example, my nginx that was configured to work with Puma looks like this:

    例如,我的nginx被配置为与Puma合作,如下图所示:

    location ~ ^/(assets|images|fonts)/(.*)$ {
        alias /var/www/foster_care/current/public/$1/$2;
        gzip on;
        expires max;
        add_header Cache-Control public;
    }
    

#7


4  

Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.

Rails 4不再生成资产的非指纹版本:样式表/样式。css将不会为您生成。

If you use stylesheet_link_tag then the correct link to your stylesheet will be generated

如果您使用的是样式表,那么将生成正确的样式表链接。

In addition styles.css should be in config.assets.precompile which is the list of things that are precompiled

除了风格。css应该在config.assets中。预编译,它是预编译的列表。

#8


2  

I'm running Ubuntu Server 14.04, Ruby 2.2.1 and Rails 4.2.4 I have followed a deploy turorial from DigitalOcean and everything went well but when I go to the browser and enter the IP address of my VPS my app is loaded but without styles and javascript.

我正在运行Ubuntu Server 14.04, Ruby 2.2.1和Rails 4.2.4,我遵循了从DigitalOcean开发的一个部署图,一切都很顺利,但当我进入浏览器并输入我的VPS的IP地址时,我的应用程序加载了,但没有样式和javascript。

The app is running with Unicorn and Nginx. To fix this problem I entered my server using SSH with my user 'deployer' and go to my app path which is '/home/deployer/apps/blog' and run the following command:

该应用程序运行的是独角兽和Nginx。为了解决这个问题,我使用SSH和我的用户“部署者”进入我的服务器,然后进入我的应用程序路径,即“/home/deployer/apps/blog”并运行以下命令:

RAILS_ENV=production bin/rake assets:precompile

Then I just restart the VPS and that's it! It works for me!

然后我重新启动VPS,就这样!跟我想的一样!

Hope it could be useful for somebody else!

希望它对别人有用!

#9


2  

change your Production.rb file line

改变你的生产。rb文件行

config.assets.compile = false

into

config.assets.compile = true

and also add

并添加

config.assets.precompile =  ['*.js', '*.css', '*.css.erb']

#10


2  

If precompile is set you DO NOT need

如果预编译是设置您不需要。

config.assets.compile = true

as this is to serve assets live.

因为这是为资产服务。

Our problem was we only had development secret key base set in config/secrets.yml

我们的问题是,我们只有在config/secrets.yml中设置开发秘密密钥库。

development:
    secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'

Need entry for production environment

需要进入生产环境。

#11


1  

The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems:

编译文件的默认匹配程序包括应用程序。js、应用程序。css和所有非js / css文件(这将自动包括所有图像资产)从应用程序/资产文件夹,包括你的宝石:

If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:

如果您有其他的清单或单独的样式表和JavaScript文件,您可以将它们添加到配置/初始化器/assets.rb中的预编译数组中。

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

http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

http://guides.rubyonrails.org/asset_pipeline.html precompiling-assets

#12


1  

Found this:

发现了这个:

The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role.

配置的配置选项。serve_static_assets已被重命名为config。serve_static_files以阐明它的角色。

in config/environments/production.rb:

在config /环境/ production.rb:

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

So set env RAILS_SERVE_STATIC_FILES or using Nginx to serving static files. Add config.serve_static_assets = true will still work, but removed in future.

因此,设置env RAILS_SERVE_STATIC_FILES或使用Nginx来服务静态文件。添加配置。serve_static_assets = true仍会工作,但在将来会被删除。

#13


0  

First of all check your assets, it might be possible there is some error in pre-compiling of assets.

首先检查您的资产,可能在预编译资产时存在一些错误。

To pre-compile assets in production ENV run this command:

要在生产环境中预编译资产,请运行以下命令:

RAILS_ENV=production rake assets:precompile

If it shows error, remove that first,

如果它显示错误,删除第一个,

In case of "undefined variable" error, load that variable file before using it in another file.

在“未定义变量”错误的情况下,将变量文件加载到另一个文件中。

example:

例子:

@import "variables";
@import "style";

in application.rb file set sequence of pre-compiliation of assets

在应用程序中。rb文件设置资产预编译序列。

example:

例子:

config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']

config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']

#14


0  

I may be wrong but those who recommend changing

我可能是错的,但那些建议改变的人。

config.assets.compile = true

config.assets.compile = true

The comment on this line reads: #Do not fallback to assets pipeline if a precompiled asset is missed.

对这一行的评论是:如果遗漏了预编译的资产,请不要退出资产管道。

This suggests that by setting this to true you are not fixing the problem but rather bypassing it and running the pipeline every time. This must surely kill your performance and defeat the purpose of the pipeline?

这表明,通过将其设置为true,您并没有解决问题,而是绕过它并每次运行管道。这肯定会扼杀您的性能,并破坏管道的目的吗?

I had this same error and it was due to the application running in a sub folder that rails didn't know about.

我有同样的错误,这是由于应用程序运行在rails不知道的子文件夹中。

So my css file where in home/subfolder/app/public/.... but rails was looking in home/app/public/...

我的css文件在home/subfolder/app/public/但是rails正在寻找home/app/public/…

try either moving your app out of the subfolder or telling rails that it is in a subfolder.

尝试将你的应用程序从子文件夹中移出,或者告诉rails它在子文件夹中。

#15


0  

it is not recommended to let capistrano do assets precompile, because it may take ages and often time out. try to do local assets precompile.

不建议让capistrano进行资产预编译,因为它可能需要很长时间,而且经常超时。尝试进行本地资产的预编译。

1st, set in config/application.rb config.assets.initialize_on_precompile = false then do local RAILS_ENV=production bin/rake assets:precompile and add those public/assets to git.

1、配置/应用程序。rb config.assets。initialize_on_precompile = false,然后执行本地RAILS_ENV=生产bin/rake资产:预编译并将这些公共/资产添加到git。

and config/environments/development.rb, change your asset path to avoid using precompiled assets:

和配置/环境/发展。rb,更改您的资产路径以避免使用预编译的资产:

config.assets.prefix = '/dev-assets'

config.assets。前缀= ' / dev-assets '

If you have db connection issue, means u have initializer that uses db. one way around it is to set a new environment by duplicate production.rb as maybe production2.rb, and in database.yml, add production2 environment with development db setting. then do

如果您有db连接问题,意味着您有使用db的初始化器。一种方法是通过重复生产来设置一个新的环境。rb也许production2。rb,在数据库中。yml,添加production2环境与开发db设置。然后做

RAILS_ENV=production2 bin/rake assets:precompile

RAILS_ENV = production2 bin /耙资产:预编译

if you are still facing some issue with assets, for example ckeditor, add the js file into config/initializers/assets.rb

如果您仍然面临一些资产问题,例如ckeditor,将js文件添加到config/initializer /assets.rb中。

Rails.application.config.assets.precompile += %w( ckeditor.js )

Rails.application.config.assets。预编译+ = % w(ckeditor。js)

#16


0  

location ~ ^/assets/ {
  expires 1y;
  add_header Cache-Control public;
  add_header ETag "";
}

This fixed the problem for me in production. Put it into the nginx config.

这解决了我在生产中的问题。放到nginx配置中。

#17


0  

Even we faced the same problem where RAILS_ENV=production bundle exec rake assets:precompile succeeded but things did not work as expected.
We found that unicorn was the main culprit here.

即使我们遇到了同样的问题,RAILS_ENV=生产bundle exec rake资产:precompile成功了,但是事情没有像预期的那样工作。我们发现独角兽是罪魁祸首。

Same as your case, even we used to restart unicorn after compiling the assets. It was noticed that when unicorn is restarted, only its worker processes are restarted and not the master process.
This is the main reason the correct assets are not served.

和您的情况一样,即使我们在编译了资产之后也会重新启动unicorn。人们注意到,当独角兽重新启动时,只有它的工人进程被重新启动,而不是主进程。这是不提供正确资产的主要原因。

Later, after compiling assets, we stopped and started unicorn so that the unicorn master process is also restarted and the correct assets were getting served.
Stopping and starting unicorn brings around 10 secs on downtime when compared to restarting the unicorn. This is the workaround that can be used where as long term solution is move to puma from unicorn.

后来,在编译资产之后,我们停止并启动了独角兽,这样独角兽的主进程也重新启动,正确的资产得到了服务。与重新启动独角兽相比,停止和启动独角兽带来了大约10秒的休息时间。这是一种变通方法,可以用它作为长期解决方案,从独角兽转移到美洲狮。

#1


97  

In rails 4 you need to make the changes below:

在rails 4中,您需要进行以下更改:

config.assets.compile = true
config.assets.precompile =  ['*.js', '*.css', '*.css.erb'] 

This works with me. use following command to pre-compile assets

这与我。使用以下命令来预编译资产。

RAILS_ENV=production bundle exec rake assets:precompile

Best of luck!

最好的运气!

#2


70  

I just had the same problem and found this setting in config/environments/production.rb:

我刚刚遇到了同样的问题,并在config/environment /production.rb中找到了这个设置。

# Rails 4:
config.serve_static_assets = false

# Or for Rails 5:
config.public_file_server.enabled = false

Changing it to true got it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?

把它改成true就可以了。默认情况下,Rails希望您将前端webserver配置为处理来自公用文件夹的文件请求,而不是将其代理到Rails应用程序。

(See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILES environment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.

(见5 Rails文档)。正如在注释中所指出的,在Rails 5中,您可以设置RAILS_SERVE_STATIC_FILES环境变量,因为默认设置是config.public_file_server。启用= ENV[' RAILS_SERVE_STATIC_FILES '].present ?。

#3


31  

In /config/environments/production.rb I had to add this:

在/ config /环境/生产。我必须加上这个:

Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )

The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

js已经预编译了,但是我还是添加了。. css和. css。erb显然不是自动发生的。^(^ _)排除——这是一个正则表达式编译泛音。

It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

有一点让人沮丧的是,文档清楚地指出,资产管道是默认启用的,但并没有阐明只适用于javascript的事实。

#4


18  

I was able to solve this problem by changing: config.assets.compile = false to
config.assets.compile = true in /config/environments/production.rb

我可以通过改变:config.assets.compile = false到config.assets.compile = true in /config/environment /production.rb来解决这个问题。

#5


9  

For Rails 5, you should enable the follow config code:

对于Rails 5,您应该启用以下配置代码:

config.public_file_server.enabled = true

config.public_file_server。启用= true

By default, Rails 5 ships with this line of config:

默认情况下,Rails 5使用这一行配置:

config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

config.public_file_server。启用= ENV[' RAILS_SERVE_STATIC_FILES '].present吗?

Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILES to true.

因此,您需要将环境变量RAILS_SERVE_STATIC_FILES设置为true。

#6


8  

There are 2 things you must accomplish to serve the assets in production:

在生产过程中,你必须完成两件事:

  1. Precompile the assets.
  2. 预编译的资产。
  3. Serve the assets on the server to browser.
  4. 在服务器上服务于浏览器。

1) In order to precompile the assets, you have several choices.

为了预编译资产,你有几个选择。

  • You can run rake assets:precompile on your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.

    您可以运行rake资产:在本地机器上预编译,提交给源代码控制(git),然后运行部署程序,例如capistrano。这不是将预编译的资产提交SCM的好方法。

  • You can write a rake task that run RAILS_ENV=production rake assets:precompile on the target servers each time you deploy your Rails app to production, before you restart the server.

    您可以编写一个rake任务,运行RAILS_ENV=生产rake资产:每次您将Rails应用程序部署到生产中,在重新启动服务器之前,在目标服务器上进行预编译。

Code in a task for capistrano will look similar to this:

capistrano任务中的代码将与此类似:

on roles(:app) do
  if DEPLOY_ENV == 'production'
    execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
  end
end

2) Now, you have the assets on production servers, you need to serve them to browser.

现在,在生产服务器上有了这些资产,您需要将它们提供给浏览器。

Again, you have several choices.

同样,你有几个选择。

  • Turn on Rails static file serving in config/environments/production.rb

    打开在config/environment /production.rb中服务的Rails静态文件。

    config.serve_static_assets = true # old
    
    or
    
    config.serve_static_files = true # new
    

    Using Rails to serve static files will kill your Rails app performance.

    使用Rails来服务静态文件会破坏Rails应用程序的性能。

  • Configure nginx (or Apache) to serve static files.

    配置nginx(或Apache)来服务静态文件。

    For example, my nginx that was configured to work with Puma looks like this:

    例如,我的nginx被配置为与Puma合作,如下图所示:

    location ~ ^/(assets|images|fonts)/(.*)$ {
        alias /var/www/foster_care/current/public/$1/$2;
        gzip on;
        expires max;
        add_header Cache-Control public;
    }
    

#7


4  

Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.

Rails 4不再生成资产的非指纹版本:样式表/样式。css将不会为您生成。

If you use stylesheet_link_tag then the correct link to your stylesheet will be generated

如果您使用的是样式表,那么将生成正确的样式表链接。

In addition styles.css should be in config.assets.precompile which is the list of things that are precompiled

除了风格。css应该在config.assets中。预编译,它是预编译的列表。

#8


2  

I'm running Ubuntu Server 14.04, Ruby 2.2.1 and Rails 4.2.4 I have followed a deploy turorial from DigitalOcean and everything went well but when I go to the browser and enter the IP address of my VPS my app is loaded but without styles and javascript.

我正在运行Ubuntu Server 14.04, Ruby 2.2.1和Rails 4.2.4,我遵循了从DigitalOcean开发的一个部署图,一切都很顺利,但当我进入浏览器并输入我的VPS的IP地址时,我的应用程序加载了,但没有样式和javascript。

The app is running with Unicorn and Nginx. To fix this problem I entered my server using SSH with my user 'deployer' and go to my app path which is '/home/deployer/apps/blog' and run the following command:

该应用程序运行的是独角兽和Nginx。为了解决这个问题,我使用SSH和我的用户“部署者”进入我的服务器,然后进入我的应用程序路径,即“/home/deployer/apps/blog”并运行以下命令:

RAILS_ENV=production bin/rake assets:precompile

Then I just restart the VPS and that's it! It works for me!

然后我重新启动VPS,就这样!跟我想的一样!

Hope it could be useful for somebody else!

希望它对别人有用!

#9


2  

change your Production.rb file line

改变你的生产。rb文件行

config.assets.compile = false

into

config.assets.compile = true

and also add

并添加

config.assets.precompile =  ['*.js', '*.css', '*.css.erb']

#10


2  

If precompile is set you DO NOT need

如果预编译是设置您不需要。

config.assets.compile = true

as this is to serve assets live.

因为这是为资产服务。

Our problem was we only had development secret key base set in config/secrets.yml

我们的问题是,我们只有在config/secrets.yml中设置开发秘密密钥库。

development:
    secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'

Need entry for production environment

需要进入生产环境。

#11


1  

The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems:

编译文件的默认匹配程序包括应用程序。js、应用程序。css和所有非js / css文件(这将自动包括所有图像资产)从应用程序/资产文件夹,包括你的宝石:

If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:

如果您有其他的清单或单独的样式表和JavaScript文件,您可以将它们添加到配置/初始化器/assets.rb中的预编译数组中。

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

http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

http://guides.rubyonrails.org/asset_pipeline.html precompiling-assets

#12


1  

Found this:

发现了这个:

The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role.

配置的配置选项。serve_static_assets已被重命名为config。serve_static_files以阐明它的角色。

in config/environments/production.rb:

在config /环境/ production.rb:

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

So set env RAILS_SERVE_STATIC_FILES or using Nginx to serving static files. Add config.serve_static_assets = true will still work, but removed in future.

因此,设置env RAILS_SERVE_STATIC_FILES或使用Nginx来服务静态文件。添加配置。serve_static_assets = true仍会工作,但在将来会被删除。

#13


0  

First of all check your assets, it might be possible there is some error in pre-compiling of assets.

首先检查您的资产,可能在预编译资产时存在一些错误。

To pre-compile assets in production ENV run this command:

要在生产环境中预编译资产,请运行以下命令:

RAILS_ENV=production rake assets:precompile

If it shows error, remove that first,

如果它显示错误,删除第一个,

In case of "undefined variable" error, load that variable file before using it in another file.

在“未定义变量”错误的情况下,将变量文件加载到另一个文件中。

example:

例子:

@import "variables";
@import "style";

in application.rb file set sequence of pre-compiliation of assets

在应用程序中。rb文件设置资产预编译序列。

example:

例子:

config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']

config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']

#14


0  

I may be wrong but those who recommend changing

我可能是错的,但那些建议改变的人。

config.assets.compile = true

config.assets.compile = true

The comment on this line reads: #Do not fallback to assets pipeline if a precompiled asset is missed.

对这一行的评论是:如果遗漏了预编译的资产,请不要退出资产管道。

This suggests that by setting this to true you are not fixing the problem but rather bypassing it and running the pipeline every time. This must surely kill your performance and defeat the purpose of the pipeline?

这表明,通过将其设置为true,您并没有解决问题,而是绕过它并每次运行管道。这肯定会扼杀您的性能,并破坏管道的目的吗?

I had this same error and it was due to the application running in a sub folder that rails didn't know about.

我有同样的错误,这是由于应用程序运行在rails不知道的子文件夹中。

So my css file where in home/subfolder/app/public/.... but rails was looking in home/app/public/...

我的css文件在home/subfolder/app/public/但是rails正在寻找home/app/public/…

try either moving your app out of the subfolder or telling rails that it is in a subfolder.

尝试将你的应用程序从子文件夹中移出,或者告诉rails它在子文件夹中。

#15


0  

it is not recommended to let capistrano do assets precompile, because it may take ages and often time out. try to do local assets precompile.

不建议让capistrano进行资产预编译,因为它可能需要很长时间,而且经常超时。尝试进行本地资产的预编译。

1st, set in config/application.rb config.assets.initialize_on_precompile = false then do local RAILS_ENV=production bin/rake assets:precompile and add those public/assets to git.

1、配置/应用程序。rb config.assets。initialize_on_precompile = false,然后执行本地RAILS_ENV=生产bin/rake资产:预编译并将这些公共/资产添加到git。

and config/environments/development.rb, change your asset path to avoid using precompiled assets:

和配置/环境/发展。rb,更改您的资产路径以避免使用预编译的资产:

config.assets.prefix = '/dev-assets'

config.assets。前缀= ' / dev-assets '

If you have db connection issue, means u have initializer that uses db. one way around it is to set a new environment by duplicate production.rb as maybe production2.rb, and in database.yml, add production2 environment with development db setting. then do

如果您有db连接问题,意味着您有使用db的初始化器。一种方法是通过重复生产来设置一个新的环境。rb也许production2。rb,在数据库中。yml,添加production2环境与开发db设置。然后做

RAILS_ENV=production2 bin/rake assets:precompile

RAILS_ENV = production2 bin /耙资产:预编译

if you are still facing some issue with assets, for example ckeditor, add the js file into config/initializers/assets.rb

如果您仍然面临一些资产问题,例如ckeditor,将js文件添加到config/initializer /assets.rb中。

Rails.application.config.assets.precompile += %w( ckeditor.js )

Rails.application.config.assets。预编译+ = % w(ckeditor。js)

#16


0  

location ~ ^/assets/ {
  expires 1y;
  add_header Cache-Control public;
  add_header ETag "";
}

This fixed the problem for me in production. Put it into the nginx config.

这解决了我在生产中的问题。放到nginx配置中。

#17


0  

Even we faced the same problem where RAILS_ENV=production bundle exec rake assets:precompile succeeded but things did not work as expected.
We found that unicorn was the main culprit here.

即使我们遇到了同样的问题,RAILS_ENV=生产bundle exec rake资产:precompile成功了,但是事情没有像预期的那样工作。我们发现独角兽是罪魁祸首。

Same as your case, even we used to restart unicorn after compiling the assets. It was noticed that when unicorn is restarted, only its worker processes are restarted and not the master process.
This is the main reason the correct assets are not served.

和您的情况一样,即使我们在编译了资产之后也会重新启动unicorn。人们注意到,当独角兽重新启动时,只有它的工人进程被重新启动,而不是主进程。这是不提供正确资产的主要原因。

Later, after compiling assets, we stopped and started unicorn so that the unicorn master process is also restarted and the correct assets were getting served.
Stopping and starting unicorn brings around 10 secs on downtime when compared to restarting the unicorn. This is the workaround that can be used where as long term solution is move to puma from unicorn.

后来,在编译资产之后,我们停止并启动了独角兽,这样独角兽的主进程也重新启动,正确的资产得到了服务。与重新启动独角兽相比,停止和启动独角兽带来了大约10秒的休息时间。这是一种变通方法,可以用它作为长期解决方案,从独角兽转移到美洲狮。