无法理解Rails 4中的资产如何在生产中发挥作用

时间:2013-07-21 00:28:16

标签: ruby-on-rails deployment ruby-on-rails-4

当我跑步时

rake assets:precompile

编译的资产写入公共/资产:

I, [2013-07-21T02:16:00.987988 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/glyphicons-halflings-white-62b67d9edee3db90d18833087f848d6e.png
I, [2013-07-21T02:16:01.037698 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png
I, [2013-07-21T02:16:01.053630 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-01-b85e542137a02bedd6c30dede873ef1e.jpg
I, [2013-07-21T02:16:01.066371 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-02-e5106e911d8a0289bfaf2ac64308a640.jpg
I, [2013-07-21T02:16:01.077879 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/marketing/slider-base/slide-03-990dccbed4c70f0118b7d30d98094811.jpg
I, [2013-07-21T02:16:01.965560 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/application-98713f9763bccfd6bc05dae422d3e242.js
I, [2013-07-21T02:16:02.068469 #13881]  INFO -- : Writing /home/jpereira/dev/saleshub/public/assets/application-a40c2cd9b0f20b2a7f3b62d45159fbb3.css

然后,我在生产中启动应用程序,使用:

RAILS_ENV=production rails s

=> Booting WEBrick
=> Rails 4.0.0 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-21 02:20:49] INFO  WEBrick 1.3.1
[2013-07-21 02:20:49] INFO  ruby 2.0.0 (2013-06-27) [x86_64-linux]
[2013-07-21 02:20:49] INFO  WEBrick::HTTPServer#start: pid=13903 port=3000

但是渲染的页面,不要寻找预编译的驴。在我得到的渲染模板中:

<!DOCTYPE html>
<html>
<head>
  <title>App Home Pagetitle>

  <link data-turbolinks-track="true" href="/stylesheets/application.css" media="all" rel="stylesheet" />
  <link href="/stylesheets/application.css" media="all" rel="stylesheet" />
  <link href="/stylesheets/marketing.css" media="all" rel="stylesheet" />
  <script src="/javascripts/application.js"></script>
  <script src="/javascripts/marketing.js"></script>
  <meta content="authenticity_token" name="csrf-param" />
  <meta content="8XQYBZWrTxmfdGvQYCK0JwQDfr2pt8si+FjW4a30SsA=" name="csrf-token" />
</head>
<body>

</body>
</html>

模板如下:

!!! 5
%html
  %head
    %title App Home Page
    =yield(:head)
    = stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
    = stylesheet_link_tag "application", params[:controller], :media => "all"
    = javascript_include_tag "application", params[:controller]
    = csrf_meta_tags
  %body
    =flash_messages(flash)
    = yield

我的production.rb如下:

WebApp::Application.configure do 
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = false
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.assets.digest = true
  config.assets.version = '1.0'
  config.log_level = :info
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new

我需要配置什么才能让stylesheet_link_tag和javascript_include_tag在生产中选择正确的资产位置?

由于

3 个答案:

答案 0 :(得分:8)

我刚写完一个类似的问题,当你的相关侧栏出现时:)

我发现blog表示这是一个错误,并通过设置

进行了莫名其妙的缓解
config.assets.compile = true

最后我answered my own question

答案 1 :(得分:1)

如果您在子文件夹中创建资源,则需要在文件前添加文件路径:

config.assets.precompile += %w( admin/admin.css admin/admin.js )。你会发现这个命令在环境/ production.rb

中被注释掉了

设置config.assets.compile = true是一个坏主意,它很慢而且没必要。

将自定义资产添加到config.assets.precompile后,运行: 终端中的bundle exec rake assets:precompile RAILS_ENV=production来编译资产。

答案 2 :(得分:0)

我相信您可能需要查看以下内容:状态:Edge Rails Guide

  

在Rails 4.0中,预编译资产不再自动从供应商/资产和库/资产中复制非JS / CSS资产。 Rails应用程序和引擎开发人员应将这些资产放在app / assets中或配置config.assets.precompile。

除此之外,我相信您可以通过在config/application.rb

中执行以下操作来指定您的JS文件
config.assets.precompile += %w( 'application.js', 'application.css',
                                 #specify CSS or JS files inside here! )

然后执行:

bundle exec rake assets:precompile RAILS_ENV=production