标签: asset-pipeline

如何在生产之前捕获rails资产管道中的错误?

我刚刚熟悉Rails 3.1,并且我花了一些时间来更新旧项目,并试图弄清楚新资产管道在开发模式与生产模式之间的表现.

默认config.assets.precompile设置仅保护application.cssapplication.js,目的是将所有内容都作为单个样式表和单个javascript文件提供.

显然有些情况下我们不希望这样,所以我们可以在该配置变量的列表中添加项目...

这是我在进入生产时遇到沙盒项目的情况:

  1. 浏览了开发中的网站,看到一切正常.资产作为单独的文件链接,并且网站显示正确.
  2. 将网站上传到我的服务器,并尝试让它在生产中运行.第一个错误是说"ie.css"(条件样式表)没有预编译.(我在Safari中,甚至不会下载这个样式表:stylesheet_link_tag在呈现页面之前从帮助程序中引发了错误.)
  3. 跑了rake assets:precompile又试了一次.
  4. 添加了违规项目config.assets.precompile并再次尝试.
  5. 将错误击倒在路边,直到它遇到另一个资产错误.
  6. GOTO 3.

不知道如何解决这个问题,我绕圈了几次,直到我认为我得到了所有的资产,并且该网站正在生产中.然后我在MSIE中尝试了它并点击另一个错误500:"belated_png_fix.js"正在被有条件地加载,并且直到那时才出现.

所以我的问题是,除了试验和错误或严重依赖集成测试之外,当资产管道发现某些样式表或javascript未添加到预编译列表中时,如何预测我的站点不会爆炸? ?

我也很好奇为什么缺少样式表资产会导致整个页面出错500而不是仅按需编译它或在请求该资产时提供404.这是故意"早早失败"的设计吗?

ruby-on-rails ruby-on-rails-3.1 asset-pipeline

8
推荐指数
1
解决办法
1614
查看次数

在部署到子URI时,Rails 3.1中的预编译资产损坏

我正在更新Rails 3应用程序以使用Rails 3.1,作为其中的一部分,我正在利用新的资产管道.到目前为止,我已经把一切都解决了一个我无法解决的相当恼人的问题.

应用程序及其所有资产在开发中运行良好,但在生产中,它使用Passenger(http://the-host/sub-uri/)部署到子URI .这个问题是资产是在部署期间预编译的,我的一个CSS(好吧,它是一个.css.scss文件)文件正在利用gem 中的image-url帮助程序sass-rails.由于在预编译过程中,路径被硬编码到预编译的CSS文件中,因此不考虑子uri:

在我的.css.scss档案中:

body { background-image: image-url("bg.png"); }
Run Code Online (Sandbox Code Playgroud)

编译application-<md5-hash-here>.css文件中的结果:

body { background-image: url(/assets/bg.png); }
Run Code Online (Sandbox Code Playgroud)

它应该是什么使它正常工作:

body { background-image: url(/sub-uri/assets/bg.png); }
Run Code Online (Sandbox Code Playgroud)

这个场景只是要求太多了吗?如果是这样,我将不得不切换回旧的非资产流水线方式,只需提供我的图像和CSS public.然而它似乎应该被考虑和解决的东西......?我错过了解决方案吗?


编辑1:我应该注意,使用erb解决方案会产生与预期相同的结果.


编辑2:回应Benoit Garret的评论

不,问题与此无关config.assets.prefix.我尝试设置(/sub-uri/assets而不是默认值/assets),但事实证明这是错误的做法 - 似乎这个设置已经与Rails应用程序的根目录有关,而不是服务器.删除它(从而恢复到默认值)已经修复了所有引起的奇怪问题(并且有很多,所有资产都进入了/sub-uri/sub-uri/assets- 这一切都非常奇怪).唯一的问题是image-url帮助者和朋友在预编译时不会获取子URI.毋庸置疑,这是合乎逻辑的,因为当它被预编译时,它不可能知道当它在Passenger下运行时,它将以这种方式配置.我的问题是如何告知它,从而最终得到预编译结果中的正确路径.如果确实可以做到的话.

我目前的解决方法是像这样引用CSS中的iamge:url(../images/bg.png)并将其放在非流水线public/images位置.很难理想,因为它不会受益于指纹和管道提供的一切.

assets passenger ruby-on-rails-3.1 sprockets asset-pipeline

8
推荐指数
1
解决办法
6713
查看次数

Ruby on Rails 3.1资产:预编译和图像

我无法让Rails 3.1资产管道预编译在生产模式下工作.它始终在SCSS中引用的图像上失败,并出现如下错误:

$ bundle exec rake assets:precompile RAILS_ENV=production 
  rake aborted!
  rails.png isn't precompiled
    (in /home/florian/AssetTest/app/assets/stylesheets/application.css.scss)
Run Code Online (Sandbox Code Playgroud)

但是当我查看public/assets目录时,图像就在那里,所以它是预编译的:

  $ ls public/assets | grep rails 
    rails-dd352fc2630e5f9aa5685ef1d7fe5997.png
Run Code Online (Sandbox Code Playgroud)

在这种情况下,SCSS文件只包含一些测试代码:

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails-3.1 asset-pipeline

8
推荐指数
1
解决办法
7803
查看次数

根据预编译期间的Rails 3.1资产管道,Twitter bootstrap有无效的CSS?

我创建了一个全新的Rails 3.1应用程序.我在app/assets/stylesheets/bootstrap.min.css中添加了twitter bootstrap CSS文件.这是相关的代码

app/assets/stylesheets/application.css(包括树,因此包含bootstrap)

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
*/
Run Code Online (Sandbox Code Playgroud)

Gemfile(包括用于编译/压缩的execjs和therubyracer)

group :development, :qa do
  gem 'execjs'
  gem 'therubyracer'
end
# Gems used only for assets …
Run Code Online (Sandbox Code Playgroud)

css ruby-on-rails-3 asset-pipeline twitter-bootstrap

8
推荐指数
2
解决办法
2816
查看次数

javascript - 未编译用于生产的Twitter引导程序jquery插件

我正在使用seyhunak的gem开发一个带有Twitter Bootstrap的3.1 Rails应用程序.

在生产模式下,我能够通过管道预编译使用基本的bootstrap CSS和JS:

RAILS_ENV=production bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

使用gem文件:

group :assets do
  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
  gem "twitter-bootstrap-rails"
end
Run Code Online (Sandbox Code Playgroud)

和application.js文件:

//= require_tree .

//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require bootstrap-tab
//= require bootstrap-modal
//= require bootstrap-dropdown
//= require bootstrap-popover
Run Code Online (Sandbox Code Playgroud)

该应用程序工作正常,除了模态和下拉列表等引导插件.这些插件作为供应商资产目录中存在的静态JavaScript库存在:

/vendor/assets/javascripts/bootstrap-dropdown.js
...
Run Code Online (Sandbox Code Playgroud)

我不确定这些文件是否正在预编译,我怎么能设法这样做?

ruby-on-rails jquery-plugins ruby-on-rails-3.1 asset-pipeline twitter-bootstrap

8
推荐指数
1
解决办法
1万
查看次数

没有路由匹配Rails中的资产/图像

在轨道上工作,图像不可见并出错.

Started GET "/assets/home.png" for 127.0.0.1 at 2012-06-19 12:23:24 +0530
Served asset /home.png - 404 Not Found (24ms)
ActionController::RoutingError (No route matches [GET] "/assets/home.png"):
Run Code Online (Sandbox Code Playgroud)

我用过命令

rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

production.rb

config.assets.compress = true
config.assets.compile = false
Run Code Online (Sandbox Code Playgroud)

application.rb中

config.assets.enabled = true
config.assets.version = '1.0'
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

asset-pipeline

8
推荐指数
1
解决办法
1万
查看次数

尽管安装了rails_12factor gem,但Rails 4资产仍然无法在Heroku中进行预编译

我最近尝试将我的Rails 3.2.13应用程序升级到新发布的4.0.0并尝试将其部署到Heroku.不幸的是,尽管遵循了这个指南,资产仍然似乎没有正确预编译.当然,我rails_12factor已经添加了gem,我还做了一些事情来将应用程序正确升级到4.0.0.它在开发模式下工作得非常好,我的所有测试仍在通过.但是,它仍然不会在Heroku中显示资产.

我从运行中注意到的一件事heroku run ls public/assets是,Heroku实际上能够预编译资产,app/assets并对cat这些文件执行命令将显示资产的编译版本.但是,如果我在浏览器上访问该文件,我总是得到404.

Heroku真的准备好了Rails 4吗?

编辑:

这是我从3.2.13升级到4所做的事情列表:

  • 删除了Rails 4中不再使用的资产组

  • 轨的更新版本从3.2.134.0.0

  • active_resource/railties从application.rb中删除require行,因为active_resource已从rails中删除为依赖项

  • 更新sass-rails和coffee-rails以使用相应的主分支,因为它使用railties 4.0.0.rc2而不是4.0.0作为依赖

  • 将devise版本更新为3.0.0.rc

  • 添加protected_attributes以简化到Rails 4的过渡,而无需切换到strong_parameters

  • 更改环境配置以添加config.eagerload并删除co​​nfig.whiny_nils以删除弃用警告.

  • 更改confirm()的语法以删除弃用警告

  • 将哈希火箭的哈希语法更改为1.9.3语法

  • 删除auto explain config以删除弃用警告

  • 使用rake rails添加bin目录:update:bin

  • 添加rails_12factorgem以便能够托管到heroku

  • 在Gemfile中为heroku添加ruby版本

编辑2

我想也值得一提的是,Heroku在预编译过程中没有任何错误,它实际上说它在预编译资产方面是成功的,这就是为什么它不起作用的奇怪之处.

ruby-on-rails heroku asset-pipeline ruby-on-rails-4

8
推荐指数
1
解决办法
2300
查看次数

percise32 vagrant box上的Rails应用程序 - 资产获取"文本文件繁忙"错误(Errno :: ETXTBSY)

尝试在Vagrant框(percise32)主机内启动Rails应用程序是Windows 7.这是我的Vagrantfile

Vagrant.configure('2') do |config|
  config.vm.box      = 'precise32'
  config.vm.box_url  = 'http://files.vagrantup.com/precise32.box'
  config.vm.hostname = 'rails-dev-box'

  config.vm.synched_folder "c:\rails_text", "/home/code"

  config.vm.network :forwarded_port, guest: 3000, host: 3003

  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = 'puppet/manifests'
    puppet.module_path    = 'puppet/modules'
  end
end
Run Code Online (Sandbox Code Playgroud)

当我尝试运行应用程序(代码正确同步)时,我在Rails服务器输出上收到以下错误:

Errno::ETXTBSY in Welcome#index

Showing /home/code/app/views/layouts/application.html.erb where line #4 raised:
Text file busy - (/home/code/tmp/cache/sass/a0a09a036cf07b1cae262d60fa989a8e24765858/welcome.css.scssc20131001-1595-f6clpt, /home/code/cache/sass/a0a09a036cf07b1cae262d60fa989a8e24765858/welcome.css.scssc)
  (in /home/code/app/assets/stylesheets/welcome.css.scss)
Run Code Online (Sandbox Code Playgroud)

有些文章建议将同步文件夹移到/ vagrant root之外是可以治愈的,但是因为我使用/ home/code似乎不是我的问题所在

想法欢迎.

ruby windows ruby-on-rails vagrant asset-pipeline

8
推荐指数
1
解决办法
5392
查看次数

活动模型序列化程序中的资产管道

我试图通过包含ActiveView :: Helpers在我的模型序列化器输出中包含一个图像资源管道URL:

class PostSerializer < ActiveModel::Serializer
  include ActiveView::Helpers

  attributes :post_image

  def post_image
    image_path "posts/#{object.id}"
  end
end
Run Code Online (Sandbox Code Playgroud)

结果是/images/posts/{id}而不是资产管道路径的有效路径,即./assets/images/posts/{id}.如何在序列化程序输出中包含有效的资产管道路径?

ruby-on-rails asset-pipeline active-model-serializers ruby-on-rails-4

8
推荐指数
2
解决办法
2049
查看次数

Heroku不会为rails4预编译资产

这里的文档说明了在Rails4中部署期间预编译资产的heroku.

但是,我没有看到预编译资产消息.

     Using thin (1.6.1)
       Using twitter-bootstrap-rails (2.2.8)
       Using uglifier (2.3.1)
       Using will_paginate (3.0.4)
       Your bundle is complete! It was installed into ./vendor/bundle
       Bundle completed (1.37s)
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
       Detected manifest file, assuming assets were compiled locally
-----> Discovering process types
       Procfile declares types -> (none)
       Default types for Ruby  -> console, rake, web, worker
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中遇到了bootstrap的问题,导航栏无法正确加载+其他一些细微差别,我认为它是资产预编译问题.

我使用的是Rails4,Ruby2.0

我在application.rb中启用了资产

config.assets.enabled = true
Run Code Online (Sandbox Code Playgroud)

手动预编译没有帮助

heroku run rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

ruby heroku asset-pipeline ruby-on-rails-4

8
推荐指数
3
解决办法
1611
查看次数