google map api 使用して
住所から緯度経度所得さして
google map 表示させようとしています。
しかし、api_key直うちではエラーでないのですが、
変数にした際にundefined method `[]' for nil:NilClass
と出てデバックしたら、google_map_apiが無効ですと出ます。
変数の定義の仕方が違うのでしょうか?
Rails.application.credentials.google_map_apiで『credentials.ymlからデータを引っ張るよお』っていうコードがいるのでしょうか?どこに記載すればいいのでしょうか?
ご教授お願いします。
$ EDITOR="vi" bin/rails credentials:editコマンドで
api_keyを変数に代入しています。
google_map_apiという変数にapi_keyが入っています。
rails c で中身確認してもしっかり入っています。
ターミナル [1] pry(main)> Rails.application.credentials.google_map_api => "xxxxxxxxxxxxxxxxxxxxxxxx"
posts.rb class Post < ApplicationRecord mount_uploader :image, ImageUploader belongs_to :user geocoded_by :address after_validation :geocode private def geocode uri = URI.escape("https://maps.googleapis.com/maps/api/geocode/json?address="+self.address.gsub(" ", "")+"&key=google_map_api") res = HTTP.get(uri).to_s response = JSON.parse(res) self.latitude = response["results"][0]["geometry"]["location"]["lat"] self.longitude = response["results"][0]["geometry"]["location"]["lng"] end end
回答1件
あなたの回答
tips
プレビュー