【问题标题】:vendor/assets/images not rendering images on rails 4供应商/资产/图像不在 Rails 4 上渲染图像
【发布时间】:2015-08-25 16:04:26
【问题描述】:

我启动了一个新应用程序并在 vendor/assets 文件夹中添加了一个主题。

我在 vendor/assets 中创建了一个名为 images 的文件夹,并将以下 sn-p 添加到我的应用程序代码中:

# inside config/initializers/assets.rb.
Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)

之后,我去控制台检查了我的资产路径

>> Rails.application.config.assets.paths
=> ["/home/renatojf/Documents/Code/bhiz/app/assets/images", "/home/renatojf/Documents/Code/bhiz/app/assets/javascripts", "/home/renatojf/Documents/Code/bhiz/app/assets/stylesheets", "/home/renatojf/Documents/Code/bhiz/vendor/assets/fonts", "/home/renatojf/Documents/Code/bhiz/vendor/assets/images", "/home/renatojf/Documents/Code/bhiz/vendor/assets/javascripts", "/home/renatojf/Documents/Code/bhiz/vendor/assets/stylesheets", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/twitter-bootstrap-rails-3.2.0/app/assets/fonts", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/twitter-bootstrap-rails-3.2.0/app/assets/images", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/twitter-bootstrap-rails-3.2.0/app/assets/javascripts", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/twitter-bootstrap-rails-3.2.0/app/assets/stylesheets", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/twitter-bootstrap-rails-3.2.0/vendor/assets/stylesheets", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/turbolinks-2.5.3/lib/assets/javascripts", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/jquery-rails-4.0.4/vendor/assets/javascripts", "/home/renatojf/.rvm/gems/ruby-2.2.2@bhiz/gems/coffee-rails-4.1.0/lib/assets/javascripts"]

如您所见,路径是加载供应商/资产/图像。 问题是主题中的所有 css 文件都会调用图像,例如:

.header-full-title {
  background-image: url("../img/colors/blue/logo_big.png");
}

当我尝试加载页面时,在开发中,我得到一个No route matches [GET] "/img/colors/blue/logo_big.png")

那么我该如何解决这个问题?

【问题讨论】:

  • 图片到底在哪里?是 vendor/assets/img/colors/blue/logo_big.png 吗?
  • 它在 vendor/assets/images/colors/blue/logo_big.png 中,我已经尝试将 assets 文件夹名称更改为 vendor/assets/img.. 同样的错误
  • 你能把CSS改成background-image: asset-url("img/colors/blue/logo_big.png");
  • 也已经这样做了。应用程序控制台停止抛出错误,但浏览器控制台上计算的 CSS 停止显示该属性。像这样:.header-full-title { float: left;溢出:隐藏;填充左:75px;背景重复:不重复;背景位置:左中;
  • 我认为你的 CSS 在评论中被截断了。你能检查你编译的 CSS 是否有 background-image 行吗?在浏览器控制台中查看源代码,或查看源代码并找到相关的 CSS 链接。

标签: ruby-on-rails ruby-on-rails-4 asset-pipeline ruby-on-rails-4.2


【解决方案1】:

是的,我在某处发现,如果 rails 使用资产管道提供静态图像,您可以直接调用它,而无需在资产路径中使用 /img。

所以.. 这行得通:

background-image: url("colors/blue/logo_big.png");

如果你在它上面再放一个文件夹..就像:

- vendor
--assets
----images
------img
--------colors

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2011-09-27
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2022-12-07
    • 2013-10-12
    相关资源
    最近更新 更多