質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

Q&A

解決済

1回答

1176閲覧

RailsにGoogle Mapを表示させたい

退会済みユーザー

退会済みユーザー

総合スコア0

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

0グッド

0クリップ

投稿2020/09/19 19:09

編集2020/09/20 05:04

前提・実現したいこと

RailsにGoogle Mapを表示させたいです。

Google Developer ConsoleからAPIキーを取得し、Viewに以下を記載しましたがエラーになりました。

<%= content_tag(:iframe, 'map', src:'https://www.google.com/maps/embed/v1/place?key=APIキー&q=' + comment.address, width: 400, height: 200, frameborder: 0) %>

仮にcomment.addressがtokyoだった場合、

<%= content_tag(:iframe, 'map', src:'https://www.google.com/maps/embed/v1/place?key=APIキー&q=tokyo', width: 400, height: 200, frameborder: 0) %>

とすると表示されるので、リンクには問題がないかと思います。

commentコントローラは以下の通りです。

def create comment = Comment.new(comment_params) comment.user_id = @current_user.id if comment.save flash[:notice] = 'Comment was posted' redirect_to posts_index_path(comment.post_id) else redirect_to posts_index_path(comment.post_id), flash: { comment: comment, error_messages: comment.errors.full_messages } end end

Rails consoleではaddressが表示されているので、保存も問題がないかと思います。

irb(main):001:0> comment = Comment.all #<Comment id: 3, post_id: 1, name: "eee", comment: "fff", user_id: 1, created_at: "2020-09-19 14:57:59", updated_at: "2020-09-19 14:57:59", address: "tokyo">]>

しかし実際に表示させようとすると以下のエラーが出てきてしまいます。

イメージ説明

すみませんが解決のためアドバイスを頂けないでしょうか。
よろしくお願いします。

追記:comment.html.erb

Google Mapは表示できていませんが、comment.nameやcomment.created_atなど他のcommentモデルのカラム情報は表示できています。

<p><%= simple_format(comment.comment) %></p> <br/> <%= content_tag(:iframe, 'map', src:'https://www.google.com/maps/embed/v1/place?key=APIキー&q=' + comment.address, width: 400, height: 200, frameborder: 0) %> <br/> <div class="p-comment__bottomLine"> <span><%= comment.name %></span> <br/><br/> <span><%= comment.created_at %></span> <br/><br/> <% if comment.user_id == @current_user.id %> <%= link_to '削除', comment, method: :delete, data: { confirm: '削除してもいいですか?' }, class: 'btn btn-primary' %> <% end %> </div>

追記:エラーログとrails consoleでの値の入力確認

Started GET "/posts/1" for IPアドレス at 2020-09-20 04:14:24 +0000 Cannot render console from IPアドレス! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#show as HTML Parameters: {"id"=>"1"} [1m[36mUser Load (1.1ms)[0m [1m[34m SELECT `users`. * FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m ↳ app/controllers/application_controller.rb:6 [1m[36mPost Load (1.0ms)[0m [1m[34m SELECT `posts`. * FROM `posts` WHERE `posts`.`id` = 1 LIMIT 1[0m ↳ app/controllers/posts_controller.rb:15 [1m[36mCACHE User Load (0.0ms)[0m [1m[34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m [["id", 1], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:16 [1m[35m (1.0ms)[0m [1m[34m SELECT COUNT( * ) FROM `likes` WHERE `likes`.`post_id` = 1[0m ↳ app/controllers/posts_controller.rb:17 Rendering posts/show.html.erb within layouts/application [1m[36mLike Load (1.0ms)[0m [1m[34m SELECT `likes`. * FROM `likes` WHERE `likes`.`user_id` = 1 AND `likes`.`post_id` = 1 LIMIT 1[0m ↳ app/views/posts/show.html.erb:26 [1m[36mComment Exists (1.0ms)[0m [1m[34m SELECT 1 AS one FROM `comments` WHERE `comments`.`post_id` = 1 LIMIT 1[0m ↳ app/views/posts/show.html.erb:43 [1m[36mComment Load (1.3ms)[0m [1m[34m SELECT `comments`. * FROM `comments` WHERE `comments`.`post_id` = 1[0m ↳ app/views/posts/show.html.erb:47 Rendered collection of comments/_comment.html.erb [8 times] (34.7ms) Rendered posts/show.html.erb within layouts/application (111.3ms) Completed 500 Internal Server Error in 236ms (ActiveRecord: 6.5ms) ActionView::Template::Error (no implicit conversion of nil into String): 2: <p><%= simple_format(comment.comment) %></p> 3: <br/> 4: 5: <%= content_tag(:iframe, 'map', src:'https://www.google.com/maps/embed/v1/place?key=APIキー&q=' + comment.address, width: '100%', height: 400, frameborder: 0) %> 6: 7: <br/> 8: <div class="p-comment__bottomLine"> app/views/comments/_comment.html.erb:5:in `+' app/views/comments/_comment.html.erb:5:in `_app_views_comments__comment_html_erb__95375582550985352_69815137906500' app/views/posts/show.html.erb:47:in `_app_views_posts_show_html_erb__991977752560510296_69815134793420'
$ rails console #<Comment id: 10, post_id: 1, name: "uuu", comment: "iii", user_id: 1, created_at: "2020-09-20 04:12:40", updated_at: "2020-09-20 04:12:40", address: "okinawa">]> $ comment = Comment.find(10) $ comment.address => "okinawa" なので値は入っているかと思います

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

本件解決しました。
原因はRailsではなくAPIキー側でした

投稿2020/09/21 14:36

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問