###前提・実現したいこと
現在お天気アプリを開発中です。
「open weather map」というAPIを使って
webページに画像を表示したいと思います。
Railsでの開発です。
1)(model)APIを叩いてアイコンを取得
2)(controller)それをURLに埋め込む
3)(view)Viewで表示
というのを考えています。
現在エラーが発生し、困っています。
何卒お力添えください。
###発生している問題・エラーメッセージ
uninitialized constant WeatherpageController
###該当のソースコード
コントローラー[weatherpage_controller.rb]
class WeatherpageController < ApplicationController
def city @weather_states_icon = Hoge::Fuga,weather_icon end
end
モデル[application_record.rb]
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
module Hoge class Fuga class << self def weather_icon weather_info = Hoge.call_api icon = weather["weather_info"][0]["icon"] # 天気のアイコン画像取得 "http://openweathermap.org/img/w/#{icon}.png " # 埋め込み end def call_api uri = URI.parse("http://api.openweathermap.org/data/2.5/weather") JSON.parse(Net::HTTP.get(uri + "?id=1850147&units=metric&appid=9221b367f5e8d78e5ce816565f709ad9")) end end end end
end
ビュー[city.html.erb]
<h1>Weatherpage#city</h1> <%= @weather_states_icon %> <p>Find me in app/views/weatherpage/city.html.erb</p>
ルーティング[routes.rb]
Rails.application.routes.draw do get 'weatherpage/city' root "weatherpage#city" end
###試したこと
rootingや構文の精査
###補足情報(言語/FW/ツール等のバージョンなど)
Ruby [2.3.0]
rails [5.1.1]
あなたの回答
tips
プレビュー