前提・実現したいこと
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" なので値は入っているかと思います
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。