我在Asset Pipeline中使用Rails 3.1和SCSS.无论如何都要访问SCSS文件中的Rails助手或控制器数据?就像是...
#main {
background-color: #{current_user.preferences.background_color}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以自己设置,$variables但我不确定如何从控制器的数据中填充它们.
我知道已有一百万个问题,但我无法理解.
我想在资产管道中包含我的大部分JS文件,但我有一些我想要有条件地加载(或仅在某些页面上).这些是大而复杂的文件,并且永远不会被95%的用户使用,所以我宁愿不为每个用户加载它们.一组JS文件用于日历,放在:
app/assets/javascripts/calendar
Run Code Online (Sandbox Code Playgroud)
所以我的清单设置为只包含顶级目录(并排除日历子目录):
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory .
Run Code Online (Sandbox Code Playgroud)
我的config/environments/production.rb:
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
# This following config is left over from previous Rails app,
# so not sure if it's now unnecessary ...
# Disable Rails's static asset server
# In production, Apache or …Run Code Online (Sandbox Code Playgroud) 我有一个在开发模式下工作得很漂亮的应用程序.为了尝试在Webrick或Passenger/apache中进行测试,我的大多数网站都会加载,直到我尝试提交ajax表单.我已经正确使用了bundle install --deployment.我已正确预编译了我的资产.但由于某种原因,我在提交远程表单时遇到以下错误.请记住,ajax实际上正在工作,因为它在数据库中创建记录.我觉得有趣的一件事是我使用带有ruby 1.9.3的gemset但我在这些错误代码中得到了对ruby 1.9.1的引用.我还包括用户控制器,以便您可以看到控制器参考.救命!
更新!! 根据创建操作的动作,编辑操作更新操作或销毁错误缺少模板用户/创建或用户/更新或用户/编辑用户/销毁等.阅读第一个答案的评论,因为我认为这是一个jcomp文件未包含在预编译过程中的问题.
Started GET "/users/5" for 24.163.20.124 at 2012-02-07 03:30:09 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"5"}
Completed 500 Internal Server Error in 58ms
ActionView::MissingTemplate (Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in:
* "/home/ctilley/Development/RatatouilleCatering/app/views"
* "/home/ctilley/Development/RatatouilleCatering/vendor/bundle/ruby/1.9.1/gems/wash_out-0.3.1/app/views"
* "/home/ctilley/Development/RatatouilleCatering/vendor/bundle/ruby/1.9.1/gems/ckeditor-3.7.0.rc2/app/views"
):
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/path_set.rb:58:in `find'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/lookup_context.rb:109:in `find'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/abstract_renderer.rb:3:in `find_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/template_renderer.rb:28:in `determine_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/template_renderer.rb:10:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/renderer.rb:36:in `render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/renderer.rb:17:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:109:in `_render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/streaming.rb:225:in `_render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:103:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:88:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/rendering.rb:16:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/instrumentation.rb:40:in `block (2 …Run Code Online (Sandbox Code Playgroud) production-environment ruby-on-rails-3.1 asset-pipeline ruby-on-rails-3.2
我有一个app/assets/javascripts/moufa.js.erb文件,用文件中的值填充config/moufa.yml.我想使用depend_onsprockets指令,以便每次更改yaml文件时,它都会重新编译js文件.
我正在开发一个宝石,我已经安装了Jasmine https://github.com/pivotal/jasmine-gem/
我所需的所有JS文件都在app/assets/javascripts/application.js的清单文件中
//= require underscore
//= require backbone
//= require_tree .//vendor
//= require_tree .//custom
//= require_tree .//templates
//= require_tree .//models
//= require_tree .//collections
//= require_tree .//views
Run Code Online (Sandbox Code Playgroud)
我的jasmine.yml文件引用了:
src_files:
- 'app/assets/javascripts/application.js'
Run Code Online (Sandbox Code Playgroud)
但是它中指定的文件都没有加载.谁知道为什么?我正在使用最新的Jasmine 1.3.2,它应该支持资产管道,这样你就不需要使用像jasmine-rails这样的东西了.有什么想法吗?
我看过几篇类似的帖子,但没有解决方案,所以我想我会提出一个更有记录的问题.
来自清单文件的我的问题 JS不包括或编译任何JS.
在本地运行我的服务器,并打开JS文件时,我看不到任何编译,只是标准的application.js清单文件:
// 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 …Run Code Online (Sandbox Code Playgroud) 在当地显示很好.但是当我上传到Heroku时,我得到以下内容:
<%= image_tag('logo-red.png') %>
Run Code Online (Sandbox Code Playgroud)
它位于资产/图像/
我没有使用Turbolinks.我是否需要在Heroku上运行命令来解决这个问题,或者是否存在一些我缺少的配置设置?
编辑1:试过跑步 heroku run rake assets:precompile RAILS_ENV=production
编辑2:heroku员工的回复:
看起来您的应用正在正确编译该图像:〜/ public/assets $ pwd/app/public/assets~/public/assets $ ls | grep logo-red logo-red-a07050d882e1dba431cef2130d39f929c611eaf8b0ec3c50db0742ddccb14d93.png
最近,在我最近部署到Heroku时,我收到警告,建议不要使用AssetSync.
remote: ###### WARNING:
remote: You are using the `asset_sync` gem.
remote: See https://devcenter.heroku.com/articles/please-do-not-use-asset-sync for more information.
Run Code Online (Sandbox Code Playgroud)
我们试图通过使用AssetSync解决的原始问题是我们的应用程序中的大型资产导致了巨大的段塞大小.在Heroku允许我们的300MB中,我们可能使用接近230MB - 尽管我们的git repo只有80MB左右.
我们通过使用AssetSync将所有已编译资产同步到通过Cloudfront提供的S3存储桶来解决此问题.在AssetSync运行之后,我们有一个钩子可以删除所有预编译资产以减少段塞大小.基本上,slug编译期间的工作流程如下所示:
链接的文章争论了一些关于它为什么不好以及使用什么的点.
使用资产同步可能会导致失败.调试很困难,不必要,并增加了额外的复杂性.不要使用它.相反,使用CDN.
[...]
您现在应该使用CDN.CDN在预编译后不会将资产复制到S3,而是从您的网站上抓取它们.以下是为什么更好的原因.
规范资产
[...]它允许您拥有存储信息的单一权威位置.如果您需要更改该信息,则只需在一个位置进行更改即可.[...]如果某人在资产同步后部署失败,会发生什么?如果有人修改了S3存储桶中的文件怎么办?现在你必须修复两个,而不是修复一个资产副本.
部署决定论
如果您使用heroku运行bash在dyno内部进行调试并运行rake资产:预编译这不仅仅是修改本地副本.它实际上也修改了S3上的副本.[...]如果网络出现故障,asset_sync的同步部分也会失败.如果您只编写文件的一部分,或者只有一半的资产被同步,该怎么办?这些事情发生了.
虽然我同意他们的观点,但问题仍然存在:当预编译资产存储在slug中时,部署Heroku应用程序的推荐方法是什么?
在Rails 5.1中,如果我们对公用文件夹中的文件使用asset_path,则会出现弃用警告.
DEPRECATION WARNING: The asset "favicon.ico" is not present in the asset pipeline.Falling back to an asset that may be in the public folder.
This behavior is deprecated and will be removed.
所以我试过了public_asset_path,但它不起作用,有帮手吗?
收到此错误,我错过了什么?
Sprockets::Rails::Helper::AssetNotFound in Static#index:
The asset "my_logo.jpg" is not present in the asset pipeline.
ActionView::Template::Error (The asset "my_logo.jpg" is not present in the asset pipeline.):
1: <%= image_tag "my_logo.jpg" %>
Run Code Online (Sandbox Code Playgroud)
我创建了一个新的 Ruby on Rails 应用程序rails new my_app# Rails 6.0.3.2
允许它安装 gems、web packer 等。
添加config.assets.compile = falseconfig/environments/development.rb
添加包含此内容的静态控制器和索引文件
应用程序/控制器/static_controller.r b
Sprockets::Rails::Helper::AssetNotFound in Static#index:
The asset "my_logo.jpg" is not present in the asset pipeline.
ActionView::Template::Error (The asset "my_logo.jpg" is not present in the asset pipeline.):
1: <%= …Run Code Online (Sandbox Code Playgroud)ruby ruby-on-rails precompile asset-pipeline rails-sprockets
asset-pipeline ×10
heroku ×2
actionview ×1
assets ×1
jasmine ×1
javascript ×1
precompile ×1
ruby ×1
sass ×1
sprockets ×1