Dark sky apiという天気情報が取得できるapiを使用したwebサイトを作っています。したいこととして天気情報を取得し、その値を作成したLocation model内に保存しそこから値を取得しその値をview pageに表示することです
生成したURLからコンソール内にて指定した地域の情報を確認までできたんですが
lib/task内で生成した値を保存しようとするとこのエラーにあたります。
rake aborted! NameError: uninitialized constant Location /Users//Documents//lib/tasks/task_sky.rake:6:in `block (2 levels) in <main>' Caused by: NameError: uninitialized constant Location
どのようにすれば表示までさせる事ができるでしょうか?
よろしくお願いします。
ruby 2.6.5 / Rails 5.2.4.1
lib/tasks
require 'darksky-ruby' namespace :task_sky do desc "get" task :sample do if Location.find(1).present? api = DarkSkyAPI.new(key: ENV['api_sky_key']) api.options = {lang: 'ja'} data = api.forecast(lat: 14.5964879, lon: -120.9095193) manira = Location.find_by(name: "manira") manira.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 10.3787569, lon: -123.7762541) cebu = Location.find_by(name: "cebu") cebu.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 8.2450404, lon: -123.00923) dapaon = Location.find_by(name: "dapaon") dapaon.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 15.1769792, lon: -120.5544492) clark= Location.find_by(name: "clark") clark.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) else api = DarkSkyAPI.new(key: ENV['api_sky_key']) api.options = {lang: 'ja'} data = api.forecast(lat: 14.5964879, lon: -120.9095193) manira = Location.create(name: "manira") manira.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 10.3787569, lon: -123.7762541) cebu = Location.create(name: "cebu") cebu.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 8.2450404, lon: -123.00923) dapaon = Location.create(name: "dapaon") dapaon.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) data = api.forecast(lat: 15.1769792, lon: -120.5544492) clark= Location.create(name: "clark") clark.update(summary: data[:currently][:summary],location_time: Time.at(data[:currently][:time])) end end end
schema.rb
ActiveRecord::Schema.define(version: 2020_03_06_070517) do create_table "locations", force: :cascade do |t| t.datetime "current_time" t.string "name" t.string "forecast" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end
view file(変数は仮ですが目的としては取得した値を表示する事です)
<%= link_to 'pages', pages_index_path %> <div class="py-4"> <div class="container py-4"> <div class="row py-4"> <div class="col-mg-3 mx-auto py-2"> <h2 class="text-center text-white">Weather</h2> <div id="weather" style="height: 500px; width: 80%; margin: 2rem auto 0;"> <p class="text-white mx-auto city"> City: <%= @manira.name %> </p> <p class="text-white mx-auto time"> Local Time: <%= @manira.current_time %> </p> <p class="text-white mx-auto weather"> Hourly weather: <%= @manira.forecast %> </p> </div> </div> <div class="col-mg-3 mx-auto py-2"> <h2 class="text-center text-white">Weather</h2> <div id="weather" style="height: 500px; width: 80%; margin: 2rem auto 0;"> <p class="text-white mx-auto"> City: <%= @cebu.name %> </p> <p class="text-white mx-auto"> Local Time: <%= @cebu.current_time %> </p> <p class="text-white mx-auto"> Hourly weather: <%= @cebu.forecast %> </p> </div> </div> <div class="col-mg-3 mx-auto py-2"> <h2 class="text-center text-white">Weather</h2> <div id="weather" style="height: 500px; width: 80%; margin: 2rem auto 0;"> <p class="text-white mx-auto"> City: <%= @clark.name %> </p> <p class="text-white mx-auto"> Local Time: <%= @clark.current_time %> </p> <p class="text-white mx-auto"> Hourly weather: <%= @clark.forecast %> </p> </div> </div> <div class="col-mg-3 mx-auto py-2"> <h2 class="text-center text-white">Weather</h2> <div id="weather" style="height: 500px; width: 80%; margin: 2rem auto 0;"> <p class="text-white mx-auto"> City: <%= @dapaon.name %> </p> <p class="text-white mx-auto"> Local Time: <%= @dapaon.current_time %> </p> <p class="text-white mx-auto"> Hourly weather: <%= @dapaon.forecast %> </p> </div> </div> </div> </div> </div>
あなたの回答
tips
プレビュー