Sass-Rails帮助者“ Image-url”,“ Asset-url”在Rails 3.2.1中不起作用
-
25-10-2019 - |
题
我在3.2.1上,带有SASS-RAILS-3.2.4和SASS-3.1.15 ...
资产管道的文档说:
asset-url("rails.png", image) becomes url(/assets/rails.png)
image-url("rails.png") becomes url(/assets/rails.png)
...
因此,我制作了以下文件:
# app/assets/stylesheets/public/omg.css.sass
body
background: asset-url('snake.gif', image)
#lol
background: image-url('snake.gif')
当我访问Localhost:3000/Assets/public/omg.css时,我得到了:
body {
background: asset-url("snake.gif", image); }
#lol {
background: image-url("snake.gif"); }
...我还尝试将文件更改为omg.css.scss,并将语法更改为:
# app/assets/stylesheets/public/omg.css.scss
body {
background: asset-url('snake.gif', image);
}
#lol {
background: image-url('snake.gif');
}
但是获得相同的结果...有人知道为什么这些帮助者不起作用吗?
解决方案
尽管文档所说的话,但Rails 3.2.6中的默认选项似乎使您可以使事情在CSS中使用更少的路径信息。例如 ../app/assets/images/rails.png
是您的示例中的引用。CSS.SCSS文件,带有类似的内容:
background: white url(rails.png) repeat-y;
您不包括 image-url
或者 asset-url
进入您的SCSS(据我所知),只是简单 url(your_image.png)
. 。这些文档似乎只是对背景中所做的事情的解释。
其他提示
当我遇到这个问题时,这是因为我没有在资产管道中包含CSS文件进行预编译。结果,它将在运行时生成。由于Sass-Rails GEM通常在:资产组中,因此在运行时生成CSS文件时,助手不可用。
尝试将以下行添加到您的应用程序.rb(或Production.RB):
config.assets.precompile += %w( public/omg.css )
我找到了解决方案 这个帖子 在将文件添加到预编译器中时,包括围绕文件命名。
如果您过去已将应用程序更新为Rails 3.1,请确保您更改了应用程序。RB文件
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
至
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
看 此铁路广播 升级到Rails 3.1并添加资产管道时。
更新: Rails 4回到了旧的方式。谢谢 亚伦·格雷!
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
您是否启用了资产管道 application.rb
?
config.assets.enabled = true
您通过将SASS样式表上的扩展设置为 .css.scss
. 。这让Rails知道在将内容排放为CSS之前先用Sass首先解析该文件。
您可能需要尝试清除 /TMP /缓存。我对Rails和Sass来说太陌生了,无法知道为什么会起作用,但是在搜索数小时后,它对我解决了同样的问题。
顺便说一句,尽管我可以看到其他SASS指令,例如设置变量并与它们进行计算,但执行。我确定有一个非常简单的解释,一旦我有时间进行跟踪。
我进行了@Ryan建议的更改,并升级了Sass-Rails:
bundle update sass-rails
SASS 3.2.6对我有用,而3.2.5却没有。
我们只是遇到了相同的问题,并通过明确要求在Gemfile中链接(即使是Action Pack的依赖性)来解决它:
group :assets do
gem 'sprockets'
gem 'sass-rails', '~> 3.2.3'
# ...
end
我不知道为什么,但是现在起作用。 ;-)
我已经猛击了几天。对我有用的唯一解决方案如下:
- 确保您的Gemfile中的Sass-Rails:开发小组。
如果未解决它,请将以下内容添加到config/ initializers/ that诸如“ horrize_sass_patch.rb”之类的新文件中:
begin require 'sass-rails' rescue end if Class.const_defined? "Sass::Script::Functions" module Sass::Script::Functions # This function exists, but doesn't automatically register declare :asset_url, [:value] declare :image_url, [:value] declare :font_url, [:value] # ... etc end end
注意:这要求您使用“ Active” Bundler加载机制,即您的应用程序。RB使用以下内容:
Bundler.require *Rails.groups(:assets => %w(development test))
...如果您的样式表在供应商中,请确保它们包含在SASS的配置中:
if config.respond_to? :sass
config.sass.load_paths << Rails.root.join('vendor', 'assets', 'stylesheets')
end
您只需添加一个尾声的斜线 /
通往路径并使用 url
就像您平时一样。
background-image: url("/assets/rails.png")