默认情况下,在Rails 3.1(RC1)下,链接在(dev)日志中往往非常冗长:
Started GET "/assets/application.css" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/application.css.scss (5ms) (pid 6303)
Started GET "/assets/application.js" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Compiled app/assets/stylesheets/default.css.scss (15ms) (pid 6303)
...
Started GET "/assets/default/header_bg.gif" for 127.0.0.1 at 2011-06-10 17:30:45 -0400
Served asset /default/header_logo.gif - 304 Not Modified (7ms) (pid 6303)
Served asset /default/header_bg.gif - 304 Not Modified (0ms) (pid 6246)
Served asset /default/footer_bg.gif - 304 Not Modified (49ms) (pid 6236)
...
Run Code Online (Sandbox Code Playgroud)
我想降低冗长程度或完全禁用它.我假设有一种干净的方法来禁用或减少日志记录的详细程度,方法是在ActiveRecord SQL语句中添加一个environment.rb或development.rb类似的配置行config.active_record.logger = nil …
ruby-on-rails ruby-on-rails-3 sprockets asset-pipeline rails-sprockets
对于我新启动的 Rails 6 应用程序,我想要一组自定义字体。我的设置如下所示:
# app/assets/stylesheets/my-font.sass
@font-face
font-family: 'my-font'
src: url('fonts/my-font.eot') format('embedded-opentype'), url('fonts/my-font.woff') format('woff'), url('fonts/my-font.ttf') format('truetype'), url('fonts/my-font.svg#my-font') format('svg')
font-weight: 400
font-style: normal
Run Code Online (Sandbox Code Playgroud)
然后在app/assets/stylesheets/fonts我的 sass 文件中引用了所有 4 个文件。
我application.sass有以下导入:@import 'my-font'.
当我运行时rails assets:precompile,它还会将所有 4 个带有后缀版本(例如my-font-7384658374658237465837246587263458.eot)的文件放在public目录中。
但是,当我运行该应用程序时,浏览器正在根目录中查找名为 的文件,该文件my-font.eot当然不存在并且是 404。这对我来说绝对是一个配置问题,但我不知道在哪里。任何帮助将非常感激。
运行rake assets:precompile因错误而结束。找不到为什么它仍然需要 sassc,sassc 仅在使用从 Sprockets 3.x 升级到 4.x 的指南迁移到 sprockets 4 时才在项目中:
gem 'sass-rails', '>= 5'
bundle update sass-rails sprockets
Run Code Online (Sandbox Code Playgroud)
然后按照 dartsass-rails 安装文档进行操作。做过:
rails tmp:clear
rake assets:clobber
rake assets:precompile
Run Code Online (Sandbox Code Playgroud)
红宝石-3.1.2
gem 'rails', '7.0.3.1'
gem 'webrick'
gem 'sprockets-rails', '~> 3.4', '>= 3.4.2'
gem 'dartsass-rails', '~> 0.4.0'
...
Run Code Online (Sandbox Code Playgroud)
manifest.js 文件内容:
//= link_tree ../images
//= link application.css
//= link application.js
//
//= link_tree ../builds
Run Code Online (Sandbox Code Playgroud)
错误日志:
** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment …Run Code Online (Sandbox Code Playgroud) sprockets rails-sprockets dart-sass ruby-on-rails-7 ruby-3.1
尝试在本地预编译资产时,我得到以下内容
RAILS_ENV=production bundle exec rake assets:precompile
rake aborted!
Sprockets::NotImplementedError: Custom asset_path helper is not implemented
Extend your environment context with a custom method.
environment.context_class.class_eval do
def asset_path(path, options = {})
end
end
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/sprockets-3.6.0/lib/sprockets/context.rb:198:in `asset_path'
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/sprockets-3.6.0/lib/sprockets/context.rb:218:in `font_path'
/Users/cman/.rvm/gems/ruby-2.2.2@jbd-ruby2.2.2/gems/font-awesome-rails-4.6.1.0/app/assets/stylesheets/font-awesome.css.erb:15:in `_evaluate_template'
Run Code Online (Sandbox Code Playgroud)
我不能为我的生活弄清楚为什么会这样 - 任何有关如何解决的建议将不胜感激!
更新
只有在我添加具有以下内容的初始化程序时,我才能进行预编译:
Rails.application.assets.context_class.class_eval do
def asset_path(path, options = {})
return ''
end
end
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做,当我推送到我的暂存环境时,Bootstrap中的glyphicons有一个空路径,因此不会渲染:
font-face{font-family:'Glyphicons Halflings';src:url("");src:url("") format("embedded-opentype"),url("") format("woff"),url("") format("truetype"),url("")
Run Code Online (Sandbox Code Playgroud)
更新2
如果我将初始化程序修改为如下所示,我在预编译bootstrap资产中获取了一个用于glypicons的路径,但它不是预编译的Glyphicon文件,而是非编译资产路径:
Rails.application.assets.context_class.class_eval do
def asset_path(path, options = {})
#return ''
"/assets/#{path}"
end
end
@font-face{font-family:'Glyphicons Halflings';src:url("/assets/bootstrap/glyphicons-halflings-regular.eot");src:url("/assets/bootstrap/glyphicons-halflings-regular.eot?#iefix") format("embedded-opentype"),url("/assets/bootstrap/glyphicons-halflings-regular.woff") format("woff"),url("/assets/bootstrap/glyphicons-halflings-regular.ttf") format("truetype"),url("/assets/bootstrap/glyphicons-halflings-regular.svg#glyphicons_halflingsregular")
Run Code Online (Sandbox Code Playgroud)
这是我的gemfile.lock,因为它与assets/sprockets …
收到此错误,我错过了什么?
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
在应用程序中,我需要从 JS 文件实例化音频文件(我正在使用 AudioContext API),或多或少如下所示:
playAudio(url) {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
let data = await fetch(url).then(response => response.arrayBuffer());
let buffer = await this.audioContext.decodeAudioData(data)
const source = this.audioContext.createBufferSource()
source.buffer = buffer
source.connect(this.audioContext.destination)
source.start()
}
Run Code Online (Sandbox Code Playgroud)
此 JS 文件是在使用 importmap 和 Sprockets 的新 Rails 7 应用程序中加载的 Stimulus 控制器。
在开发环境中,JS 可以猜测路径,因为 Sprockets 将以规范名称(例如/assets/audio/file.wav)来服务资产。然而,在生产中,在资源预编译期间,Sprockets 在文件名后添加一个哈希值,并且只能使用/assets/audio/file-f11ef113f11ef113f113.wav.
该文件名无法进行硬编码,因为它取决于预编译(从技术上讲,我可能可以使用哈希对路径进行硬编码,因为文件不会经常更改,但我不想对此哈希进行任何假设)。
除公共文件夹中的其他资产外,Sprockets 在预编译期间生成的清单中引用了该文件。使用Rails.application.assets_manifest.files我可以访问清单数据并安全地进行映射。
这是我为此编写的助手:
def audio_assets_json
audio_assets = Rails.application.assets_manifest.files.select do |_key, file|
file['logical_path'].start_with?('audio/')
end
JSON.pretty_generate(
audio_assets.to_h { |_k, f| [f['logical_path'], asset_url(f['logical_path'])] } …Run Code Online (Sandbox Code Playgroud) 我收到此错误:
ActionView::Template::Error (Asset `application.css` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.
//= link application.css
and restart your server):
Run Code Online (Sandbox Code Playgroud)
Rails 5.1.4
链轮(4.0.0.beta5、4.0.0.beta4、3.7.1)
链轮轨道(3.2.1、3.2.0、2.3.3)
Manifest.js
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
Run Code Online (Sandbox Code Playgroud)
Assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset …Run Code Online (Sandbox Code Playgroud) 我正在将Rails 5.2应用程序迁移到ActionMailbox,ActionText和多个数据库的6.0。但是,我不知道webpack,而是想使用Sprockets。
如何正确从Rails 6中删除webpack并安装Sprockets?rails new app立即安装webpack文件。有没有一种方法可以轻松轻松地默认链轮?
Rails(Ruby)被认为是配置的惯例,但是Webpack(Javascript)通过增加许多配置和复杂性来取消它。
我正在启动一个新的 Rails 6 应用程序。如果我理解正确的话,Webpack(er) ( gem webpacker) 已经取代了 Sprockets 作为包含/缩小 JS ( source )的新标准。此外,Rails 6 现在都需要Node.js 和 Yarn。
我是否正确地假设 Node.js 和 Yarn 依赖项仅仅是由于包含了 Webpack,或者 Rails 6 的其他组件也需要它们?
从 Rails 6 应用程序中删除 Webpack 和 Node 和 Yarn 并继续使用 Rails Asset Pipeline(除了缺少 Webpack 功能)是否有任何可能的缺点?
sprockets ×4
ruby ×3
precompile ×2
assets ×1
dart-sass ×1
import-maps ×1
javascript ×1
ruby-3.1 ×1
webpack ×1