我有一个正在运行的Rails 3.2.1应用程序,我正在通过Capistrano部署deploy/assets它来运行deploy:assets:precompile任务.
一切都很好,除了编译本身很慢.我没有那么多CSS和JavaScript(总共大约8200行).
编译资产通常需要大约1-3分钟,大约占整个部署时间的90%.
有没有办法优化这个?也许使用不同的程序来编译资产或以某种方式优化它?
我正在Linode 512上运行应用程序1.9.2-p290,Rails 3.2.1并使用therubyracergem如果有任何相关性.
ruby deployment ruby-on-rails asset-pipeline ruby-on-rails-3.2
rails资产管道不包括application.js中所需的文件.
呈现给浏览器的唯一javascript文件是application.js,并且require行不会被编译为包含标记,因为它们应该是:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS …Run Code Online (Sandbox Code Playgroud) 我已经开始开发简单的rails应用程序了.经过几个小时的工作后,我注意到删除的css仍以某种方式应用于网页.
为了解决这个问题,我多次执行了以下操作:
但没有变化.这很奇怪 - 新的css定义已经应用,但我删除的那些仍然存在.所以,我放弃了,决定创建新项目.
我已经设置了新项目(它的脚手架与第一个脚手架相同),当我打开其中一个视图时,旧项目的样式也被应用了.我决定再次查看http://guides.rubyonrails.org/asset_pipeline.html并找出该设置
#Expands the lines which load the assets
config.assets.debug = false
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.但这个选项究竟做了什么呢?当这是真的时,为什么旧的项目css被应用?
我刚刚将我的应用程序升级Rails 4.2.7为Rails 5.0.0.1.我使用RailsDiff来确保我已经覆盖了所有内容,我相信我做到了.到目前为止,一切都运行良好,直到加载我的应用程序.
现在我看到这个错误:
Sprockets::ArgumentError at /
require_tree argument must be a directory
Run Code Online (Sandbox Code Playgroud)
这是我的application.css:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file …Run Code Online (Sandbox Code Playgroud) 在rails 3.1中使用资产管道时,它会创建一个默认值application.js:
//= require jquery
//= require jquery_ujs
//= require_tree .
Run Code Online (Sandbox Code Playgroud)
但我什么时候需要包括我的所有JavaScript?在大多数情况下,我们为不同的控制器/视图使用不同的javascrips?
是否可以将SWF文件移动到资产管道,如果可能,如何才能完成?
而不是让页面包含一个样式标记,其中包含从哪里获取css的链接,我可以使用rails的stylesheet_link_tag帮助方法将其添加到我的视图中,我希望直接在页面内部使用css内联.
这是我到目前为止提出的:
%style(type="text/css")=File.read(physical_asset_path("email.css"))
Run Code Online (Sandbox Code Playgroud)
但我找不到任何轨道的辅助方法,它给了我一个资产的物理路径 - physical_asset_path只是我发明的虚拟方法.
有人知道如何在使用rails 3.2.x时获取资产的物理路径吗?
是否有更简单/更好的方式来获取样式表 - 来自共轨资产路径中的css文件 - 内联?
使用案例:大多数电子邮件客户端在没有用户确认的情况下不访问外部源(如css,图像).因此,要正确显示电子邮件,我需要将CSS嵌入电子邮件的HTML中.
我在rails 3应用程序中使用font-awesome,在开发模式下一切正常,但是当我推送到Heroku时,Firefox无法呈现图标,相反,我看到了:

这就是我所做的:
将以下内容添加到font-awesome.css.scss的顶部:**
// font-awesome.css.scss
@font-face {
font-family: 'FontAwesome';
src: font-url("fontawesome-webfont.eot");
src: font-url("fontawesome-webfont.eot?#iefix") format("eot"),
font-url("fontawesome-webfont.woff") format("woff"),
font-url("fontawesome-webfont.ttf") format("truetype"),
font-url("fontawesome-webfont.svg#FontAwesome") format("svg");
font-weight: normal;
font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)
然后我把它放在application.rb中:
# application.rb
config.assets.paths << Rails.root.join("app", "assets", "fonts")
config.assets.precompile += %w( .svg .eot .woff .ttf )
Run Code Online (Sandbox Code Playgroud)
最后我将所有4个字体文件放入app/assets/fonts.
我真的很想知道我在这里做错了什么.
我希望我用ActionMailer发送的电子邮件包含链接回我的应用程序的图像.在我的开发环境中,例如:
<img src="http://myfullappurl.dev/assets/myimage.png">
Run Code Online (Sandbox Code Playgroud)
我在我的开发中有这个.rb
config.action_controller.asset_host = 'myfullappurl.dev'
config.action_mailer.asset_host = config.action_controller.asset_host
config.action_mailer.default_url_options = { host: 'myfullappurl.dev', only_path: false }
Run Code Online (Sandbox Code Playgroud)
但我不能让我的邮件模板以这些方式呈现完整的URL:
asset_path('myimage.png')
asset_path('myimage.png', only_path: false)
image_url('myimage.png')
Run Code Online (Sandbox Code Playgroud)
通过这样的方式回答了很多关于这个主题的类似问题:
"#{request.protocol}#{request.host_with_port}#{asset_path('image.png')}"
Run Code Online (Sandbox Code Playgroud)
但是因为我的邮件是与Sidekiq异步发送的,所以没有任何request对象,如果它们是在控制器中同步发送的话.
有没有明显的东西我想念,或者我是否必须与Rails作斗争才能做到这一点?我正在使用Rails 4,但我确信在3.1/3.2中有效的任何东西都可以.
推荐链:
它被报告为bootstrap-rails gem的一个问题:https://github.com/anjlab/bootstrap-rails/issues/91
宝石
gem 'sass-rails', '>= 3.2' # sass-rails needs to be higher than 3.2
gem 'bootstrap-sass', '~> 3.1.1'
Run Code Online (Sandbox Code Playgroud)
安装(在资产组的外部和内部尝试),application.css.scss包含
@import "bootstrap";
Run Code Online (Sandbox Code Playgroud)
只要.
尝试bundle exec rake assets:precompile时会出现此错误:
rake aborted!
Undefined variable: "$alert-padding".
(in /home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/bootstrap-sass-3.1.1.0/vendor/assets/stylesheets/bootstrap/_alerts.scss)
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/bootstrap-sass-3.1.1.0/vendor/assets/stylesheets/bootstrap/_alerts.scss:10
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/script/variable.rb:49:in `_perform'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/script/node.rb:40:in `perform'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/perform.rb:298:in `visit_prop'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/base.rb:37:in `visit'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/perform.rb:100:in `visit'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/base.rb:53:in `block in visit_children'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/base.rb:53:in `map'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/base.rb:53:in `visit_children'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/perform.rb:109:in `block in visit_children'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/perform.rb:121:in `with_environment'
/home/joe/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sass-3.2.7/lib/sass/tree/visitors/perform.rb:108:in `visit_children' …Run Code Online (Sandbox Code Playgroud) asset-pipeline ×10
ruby ×4
actionmailer ×1
deployment ×1
flash ×1
font-awesome ×1
font-face ×1
fonts ×1
jruby ×1
sprockets ×1