【问题标题】:Rails 6 image not showing in Heroku stagingRails 6 图像未在 Heroku 暂存中显示
【发布时间】:2020-01-23 18:49:28
【问题描述】:

我在 Welcome Controller 中定义了一些背景图像,因此根据图像更改的视图,在本地工作但当我在 Heroku 中部署时,它停止显示背景图像。

应用程序位于 rails 6 中,图像并从 assets 文件夹中获取。 背景图片正在课程视图中加载

标题部分,我正在加载图像:

<section style="background-image:url(<%= @background_image%>)" class="banner <%= 'short-banner' unless @index%>" role="banner">

欢迎控制器:

  def index
    @background_image = '/assets/computer_low_res.jpg'
    @title = 'BOGOTA BOOTCAMP'
  end

  def learn
    @background_image = '/assets/two_computers_low_res.jpg'
    @title = 'Try some Ruby'
  end

  def courses
    @background_image = 'https://s3.us-east-2.amazonaws.com/bogota-bootcamp/about-program.jpg'
    @title = 'Courses'
  end

【问题讨论】:

  • 访问courses url,是否显示图像?
  • 是的,一个节目,可能是因为托管在 AWS 中

标签: ruby-on-rails image heroku assets ruby-on-rails-6


【解决方案1】:

查看env/production.rb

将这一行设为true 并再次部署。让我知道...

#config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.public_file_server.enabled = true

【讨论】:

  • 不起作用,这出现在 heroku 控制台中:2019-09-23T19:05:12.453510+00:00 heroku[router]: at=info method=GET path="/assets/our_team .jpg" host=bogota-bootcamp-staging.herokuapp.com request_id=e5aaedcc-801d-42b7-aa02-df6fd13e0f36 fwd="181.48.147.210" dyno=web.1 connect=0ms service=3ms status=404 bytes=1781 协议=https
【解决方案2】:

解决了! 更改标题:

<section style="background-image:url(<%= asset_path(@background_image)%>)" class="banner <%= 'short-banner' unless @index%>" role="banner">

控制器:

 def index
    @background_image = 'computer_low_res.jpg'
    @title = 'BOGOTA BOOTCAMP'
  end

  def learn
    @background_image = 'two_computers_low_res.jpg'
    @title = 'Try some Ruby'
  end

  def courses
    @background_image = 'https://s3.us-east-2.amazonaws.com/bogota-bootcamp/about-program.jpg'
    @title = 'Courses'
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2021-05-28
    • 2021-03-30
    • 1970-01-01
    • 2020-12-25
    • 2015-09-05
    相关资源
    最近更新 更多