html - Use ERB to access images NOT asset pipeline -


i want display dynamically chosen image, within html call upon variable @background_img, contains url specific picture. however, doing

<body style='background-image: url(<%=@background_img%>);'> 

simply refuses display image background. misinterpreting how erb works, because wouldn't rails precompile css , end working html image fetch? using chrome developer tools when previewing app reveals url(), , empty parameter can't fetch image.

edit: wanted add rather not have download images, keep urls have prepared.

this weatherman class:

require 'rest-client'  class weatherman   #images within accessible data structures, designed 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 

so sorry formatting, on mobile right now.

now, controller class:

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 

the problem seems @background_img not populated.

the reason seems weatherman class. attempt rectify issue...


controller

if you're calling @background_img on body tag, means it's accessible @ every controller action. thus, instead of declaring in solitary home action, need make available each time load views:

#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 

--

class

the main issue see class not giving value. i'll attempt refactor class, although can't promise anything:

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)      #### should return image ####      #### below 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 

--

view

you need make sure you're getting returned data controller in order populate in view.

because getimg method returning nil, you're getting nil response. have amended for now 1 of flickr links have included in class.

if have returned image, following should work:

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

because @background_img external url, above should work. if using file asset_pipeline, you'd want use image_url etc


Comments