我想显示动态选择的图像,因此在html中我调用变量@background_img
,其中包含特定图片的url。但是,做
<body style='background-image: url(<%=@background_img%>);'>
只是拒绝显示背景图像。我是否误解了ERB是如何工作的,因为Rails不会简单地预编译CSS并最终得到一个有效的HTML图像获取?在预览我的应用时使用Chrome开发者工具会显示url()
,显然空参数无法获取图片。
编辑: 只是想补充一点,我宁愿不必下载图像,但保留我已经准备好的网址。
这是WeatherMan类:
require 'rest-client'
class WeatherMan
#images within accessible data structures, designed to be expandable
def initialize
@hot = ['https://farm2.staticflickr.com/1515/23959664094_9c59962bb0_b.jpg']
@rain = ['https://farm8.staticflickr.com/7062/6845995798_37c20b1b55_h.jpg']
end
def getWeather(cityID)
response = JSON.parse RestClient.get "http://api.openweathermap.org/data/2.5/weather?id=#{cityID}&APPID=bd43836512d5650838d83c93c4412774&units=Imperial"
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)
if temp <= 100 #CHANGE!!!
return @rain[rand(@rain.length)]
elsif temp <= 32
return nil
elsif temp <= 50
return nil
elsif temp <= 75
return nil
elsif temp <= 105
return nil
end
end
end
很抱歉格式化,现在在手机上。
现在,控制器类:
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
答案 0 :(得分:1)
问题似乎是subdirectory_1.aln
未填充。
的原因似乎是您的subdirectory_2.aln
课程。我会尝试纠正这个问题......
<强>控制器强>
如果您在正文标记上调用@background_img
,则表示可以通过每个控制器操作访问它。因此,您不必在单独的Weatherman
操作中声明它,而是每次加载视图时都需要使它可用:
@background_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
方法正在返回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
,因此您收到getImg
响应。我已经使用您在课程中包含的nil
个链接之一修改了 for now 。
如果您总是有返回的图像,则以下内容应该有效:
nil
由于您的flickr
是外部网址,因此以上内容应该有效。如果您使用的是#app/views/layouts/application.html.erb
<body style='background-image: url(<%= @background_img %>);'>
中的文件,则需要使用image_url
等