Html 使用ERB访问图像而不是资产管道

Html 使用ERB访问图像而不是资产管道,html,ruby-on-rails,erb,Html,Ruby On Rails,Erb,我想显示一个动态选择的图像,因此在html中我调用变量@background\u img,该变量包含特定图片的url。但是, <body style='background-image: url(<%=@background_img%>);'> 很抱歉,现在在手机上格式化 现在,控制器类: load File.expand_path("../../data_reader.rb", __FILE__) load File.expand_path("../../weathe

我想显示一个动态选择的图像,因此在html中我调用变量
@background\u img
,该变量包含特定图片的url。但是,

<body style='background-image: url(<%=@background_img%>);'>
很抱歉,现在在手机上格式化

现在,控制器类:

load File.expand_path("../../data_reader.rb", __FILE__)
load File.expand_path("../../weatherstation.rb", __FILE__)
class PagesController < ApplicationController
  def home
   # `sudo python /home/pi/Documents/coding/raspberryPI/weatherStation/app/led_blink.py`
    server = WeatherMan.new
    @outside_data = server.getWeather(4219934)
    @sensor_temp = DRead.read_data(File.expand_path('../../data.txt', __FILE__), 'temperature')
    @sensor_temp = (@sensor_temp.to_f * (9.0/5) + 32).round(2)
    @background_img = server.getImg(@outside_data[:temp])
  end
end
load File.expand\u path(“../../data\u reader.rb”,\u文件\u)
加载文件。展开\u路径(“../../weatherstation.rb”,\u文件\u)
类PagesController<应用程序控制器
def主页
#`sudo python/home/pi/Documents/coding/raspberryPI/weatherStation/app/led_blink.py`
服务器=WeatherMan.new
@外部数据=server.getWeather(4219934)
@传感器_temp=DRead.read_数据(File.expand_路径('../../data.txt',__文件,''temperature'))
@传感器温度=(@sensor_temp.to_f*(9.0/5)+32)。四舍五入(2)
@后台\u img=server.getImg(@outside\u data[:temp])
结束
结束

问题似乎是
@background\u img
没有填充

原因似乎是你的
天气预报员
课程。我将试图纠正这个问题


控制器

如果你在你的身体标签上调用
@background\u img
,这意味着每次控制器动作都可以访问它。因此,您不需要在单独的
home
操作中声明它,而是需要在每次加载视图时使其可用:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   before_action :set_background

   private

   def set_background
      server = WeatherMan.new
      @outside_data   = server.getWeather(4219934)
      @sensor_temp    = DRead.read_data(File.expand_path('../../data.txt', __FILE__), 'temperature')
      @sensor_temp    = (@sensor_temp.to_f * (9.0/5) + 32).round(2)
      @background_img = server.getImg(@outside_data[:temp])
   end
end
--

查看

您需要确保从控制器获取返回的数据,以便在视图中填充它

由于您的
getImg
方法返回
nil
,因此您将得到
nil
响应。现在,我已经用你在课程中包含的
flickr
链接之一对其进行了修改

如果您始终有一个返回的映像,则以下操作应有效:

#app/views/layouts/application.html.erb
<body style='background-image: url(<%= @background_img %>);'>
#app/views/layouts/application.html.erb

因为您的
@background\u img
是一个外部URL,所以上面的方法应该有效。如果您使用的是
资产_管道中的文件
,那么您需要使用etc

您确定var的值正确吗?我不确定您是否可以将ERB标记放入另一个HTML标记中。您可以重写为
,它应该可以工作。那么您如何填充
@background\u img
?你提到它不在资产管道中,但没有告诉我们它在哪里的任何信息from@RichPeck后台\u img填充在控制器中,分配给数组的随机索引。该数组只有指向URL的字符串文本。@taglia我将其更改为该字符串,但没有效果。替换没有发生,结果是
背景图像:url(#{@background\u img})帅哥!终于让代码开始工作了。我认为这个问题确实是由于WeathMan
getImg
方法造成的——它返回零,因为开放天气图中的一个错误给了我开尔文(276等)的温度,而我的代码显然不是为处理这个问题而设计的。因此,它默认为零。谢谢你的帮助!
require 'rest-client'
class WeatherMan

  @@static = {
    hot:  'https://farm2.staticflickr.com/1515/23959664094_9c59962bb0_b.jpg',
    rain: 'https://farm8.staticflickr.com/7062/6845995798_37c20b1b55_h.jpg'
  }

  def getWeather(cityID)
     response = JSON.parse RestClient.get weather_url(cityID)  
     return {
        temp:          response['main']['temp'].to_f.round,
        cloudiness:    response['clouds']['all'].to_f.round,
        humidity:      response['main']['humidity'].to_f.round,
        windiness:     response['wind']['speed'],
        condition_id:  response['weather'][0]['id'].to_f,

        condition_name:        response['weather'][0]['main'],
        condition_description: response['weather'][0]['description'],
        condition_img:         response['weather'][0]['icon']
     }
  end

  def getImg(temp)
     #### This should return the image ####
     #### Below is a test              ####
     @@static[:hot]
  end

  private

  def weather_url city
     "http://api.openweathermap.org/data/2.5/weather?id=#{city}&APPID=bd43836512d5650838d83c93c4412774&units=Imperial"
  end
end
#app/views/layouts/application.html.erb
<body style='background-image: url(<%= @background_img %>);'>