【问题标题】:Images do not load in Rails development Environment在 Rails 开发环境中无法加载图像
【发布时间】:2015-07-28 15:34:39
【问题描述】:

我有以下代码行:

<img src="<%= image_url ("/slides/hotel.jpg") %>" style="opacity:0;" alt="slidebg1" data-bgfit="cover" data-bgposition="left bottom" data-bgrepeat="no-repeat">

宝石文件:

source https://rubygems.org
gem 'rails', '4.2.3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'pg', '~> 0.18.2'
gem 'coffee-script-source', '1.8.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'simple_form'
gem 'headjs-rails'

group :development, :test do
  gem 'debugger'
  gem 'activerecord-postgresql-adapter'  gem 'web-console', '~> 2.0'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Development.rb:

config.assets.debug = false
config.assets.compile = true
config.assets.enabled = true
config.serve_static_assets  = true

我在 Windows 8 中运行,我提供了 rake assets:precompile,它完成得很好。但是图像不加载。我确定该图像存在于该指定目录下。就我而言,图像存在于assets/images/slides/hotel.jpg 下。我曾尝试将文件路径指定为/assets/images/slides/hotel.jpg/images/slides/hotel.jpg/slides/hotel.jpg,但这三个都不起作用。

错误:

Started GET "/slides/hotel.jpg" for ::1 at 2015-07-28 21:03:04 +0530
ActionController::RoutingError (No route matches [GET] "/slides/hotel.jpg")

【问题讨论】:

  • 你能显示你的config/environments/production.rb文件内容吗
  • config.cache_classes = true config.eager_load = true config.consider_all_requests_local = false config.action_controller.perform_caching = true config.assets.compile = true config.assets.compile = true config.serve_static_files = ENV[ 'RAILS_SERVE_STATIC_FILES'].present?
  • 你有config.assets.digest = true
  • @Nermin:是的,它可用。

标签: ruby-on-rails ruby-on-rails-3 erb


【解决方案1】:

在您的情况下,图像存在于assets/images/slides/hotel.jpg 下,因此image_url 返回的路径必须是/images/slides/hotel.jpg(有关guide 中的资产路径的更多信息)

解决方案:

<img src="<%= image_url('slides/hotel.jpg') %>" style="opacity:0;" alt="slidebg1" data-bgfit="cover" data-bgposition="left bottom" data-bgrepeat="no-repeat">
#                        ^---- Just remove the "/" here

我是如何发现的:

小红包,帮你一探究竟,下次跑类似情况

从您的 rails 应用程序目录,在终端中,启动 rails c

# Include the helper whom define image_url
irb(main):001:0> include ActionView::Helpers::AssetUrlHelper
=> Object

# Test the output of your actual code
irb(main):002:0> image_url "/slides/hotel.jpg"
=> "/slides/hotel.jpg"
# As you can see the path is incorrect because it does not start with '/images'

# Test the output after removing the heading slash
irb(main):003:0> image_url "slides/hotel.jpg"
=> "/images/slides/hotel.jpg"
# Correct output as the path starts with '/images'

【讨论】:

  • 感谢您的努力。不幸的是,我仍然无法将我的图像发送到该站点。在 2015-08-01 16:29:31 +0530 [2015-08-01 16:29:31] 开始为 ::1 获取“/javascripts/default.js” 错误 Errno::ECONNABORTED:已中止建立的连接通过主机中的软件。 c:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:80:in eof?' c:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:80:in run' c:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in `阻塞在 start_thread'
  • 卡在这里很久了。
  • 这显然是另一个问题。重新启动服务器并重试
  • 我丢失了宝石,我可以解决这个问题。非常感谢@Benjamin:我给你应得的赏金。干杯:)
  • Stack Overflow 说还有 13 个小时。等等。
【解决方案2】:

你为什么不用image_path

例如

<%= image_path "/slides/hotel.jpg", style:"opacity:0;", alt:"slidebg1", 'data-bgfit'=>"cover", 'data-bgposition'=>"left bottom" 'data-bgrepeat'=>"no-repeat"%>

【讨论】:

  • 运气不好,Nermin。问题占上风。
【解决方案3】:

我会尝试其他两种变体:

(1) image_url ("slides/hotel.jpg")(我无法想象这是基本的,image_url 正在添加一个额外的斜线......)

(2)asset_url ("hotel.jpg")

【讨论】:

    【解决方案4】:

    试试这个

    在 application.rb 中,添加以下行:

    config.assets.enabled = true
    

    如果幻灯片目录在资产目录中,那么

    config.assets.paths << Rails.root.join("app", "assets", "slides")
    

    如果幻灯片目录在 assets/images 目录中,那么

    config.assets.paths << Rails.root.join("app", "assets", "images", "slides")
    

    然后尝试加载图片

    <%= assets_path "/slides/hotel.jpg", style:"opacity:0;", alt:"slidebg1", 'data-bgfit'=>"cover", 'data-bgposition'=>"left bottom" 'data-bgrepeat'=>"no-repeat"%>
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 2017-09-10
      相关资源
      最近更新 更多