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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

Q&A

解決済

1回答

239閲覧

RailsでAjaxでいいね機能

hrmths

総合スコア17

Ruby on Rails 5

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

Ajax

Ajaxとは、Webブラウザ内で搭載されているJavaScriptのHTTP通信機能を使って非同期通信を利用し、インターフェイスの構築などを行う技術の総称です。XMLドキュメントを指定したURLから読み込み、画面描画やユーザの操作などと並行してサーバと非同期に通信するWebアプリケーションを実現することができます。

0グッド

1クリップ

投稿2018/04/29 08:42

編集2018/04/30 23:23

概要

https://teratail.com/questions/88802

こちらの方と同様、railsとjsを使ったお手軽「いいね♡機能」 を参考にしながら、コメントにいいね機能(comment_like)を作っています。

こちらも参考にさせて頂きながら実装したのですが、リロードしないといいねが更新されず困っています。どなたかアドバイスいただけますでしょうか?

環境は、以下の通りです。

  • Ruby 2.3.0
  • Rails 5.0.6

ソースコード

_comment_like.html.erb

Ruby

1<% if user_signed_in? %> 2 <% if current_user.comment_liking?(comment) %> 3 <%= button_to comment_comment_like_path(comment, comment_id: comment.id), method: :delete, id: "comment-like-button", remote: true do %> 4 <%= content_tag :span, "", class: "glyphicon glyphicon-heart" %> 5 <span> 6 <%= comment.comment_likes_count %> 7 </span> 8 <% end %> 9 <% else %> 10 <%= button_to comment_comment_likes_path(comment), id: "comment-like-button", remote: true do %> 11 <%= content_tag :span, "", class: "glyphicon glyphicon-heart-empty" %> 12 <span> 13 <%= comment.comment_likes_count %> 14 </span> 15 <% end %> 16 <% end %> 17<% else %> 18 <%= content_tag :span, "", class: "glyphicon glyphicon-heart-empty" %> 19 <span> 20 <%= comment.comment_likes_count %> 21 </span> 22<% end %>

create.js.erb

Ruby

1$("#comment-like-<%= @comment.id %>").html("<%= render 'comment_like', comment: @comment, comment_like: @comment_like %>");

destroy.js.erb

Ruby

1$("#comment-like-<%= @comment.id %>").html("<%= render 'comment_like',comment: @comment %>");

comment_likes_controller.erb

Ruby

1class CommentLikesController < ApplicationController 2 before_action :require_user_logged_in 3 before_action :set_comment 4 5 def create 6 @comment_like = CommentLike.create(user_id: current_user.id, comment_id: params[:comment_id]) 7 end 8 9 def destroy 10 comment_like = CommentLike.find_by(user_id: current_user.id, comment_id: params[:comment_id]) 11 comment_like.destroy 12 end 13 14 private 15 16 def set_comment 17 @comment = Comment.find(params[:comment_id]) 18 end 19end

application.html.erb

Ruby

1 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous"> 2 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 3 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 4 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Bootstrapを利用しており、application.html.erbも関係しそうな部分だけ記載させていただきます。

よろしくお願いいたします。

--4/29追記--

サーバーログは特にエラーは吐いていない。

Started POST "/comments/9/comment_likes" for 10.240.1.50 at 2018-04-29 09:57:05 +0000 Cannot render console from 10.240.1.50! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by CommentLikesController#create as JS Parameters: {"authenticity_token"=>"sample_token_hogehoge", "comment_id"=>"9"} User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 Comment Load (0.8ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`id` = 9 LIMIT 1 (0.3ms) BEGIN User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 Comment Load (62.8ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`id` = 9 LIMIT 1 SQL (12.0ms) INSERT INTO `comment_likes` (`user_id`, `comment_id`, `created_at`, `updated_at`) VALUES (1, 9, '2018-04-29 09:57:05', '2018-04-29 09:57:05') SQL (0.7ms) UPDATE `comments` SET `comment_likes_count` = COALESCE(`comment_likes_count`, 0) + 1 WHERE `comments`.`id` = 9 (8.2ms) COMMIT Rendering comment_likes/create.js.erb Comment Exists (0.7ms) SELECT 1 AS one FROM `comments` INNER JOIN `comment_likes` ON `comments`.`id` = `comment_likes`.`comment_id` WHERE `comment_likes`.`user_id` = 1 AND `comments`.`id` = 9 LIMIT 1 Rendered comment_likes/_comment_like.html.erb (6.4ms) Rendered comment_likes/create.js.erb (13.4ms) Completed 200 OK in 146ms (Views: 23.1ms | ActiveRecord: 87.7ms)

--5/1追記--
create.js.erbに console.log("hello"); と記述してもサーバーログになにも表示されない。create.js.erbが読み込まれていない・・?

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

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

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

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

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

guest

回答1

0

自己解決

このようにしたらいけた。 escape_javascript の記述がなかったことが原因のよう。

create.js.erb

Ruby

1$("#comment-like-<%= @comment.id %>").html("<%= escape_javascript(render 'comment_like', comment: @comment, comment_like: @comment_like) %>");

delete.js.erb

Ruby

1$("#comment-like-<%= @comment.id %>").html("<%= escape_javascript(render 'comment_like',comment: @comment) %>");

また、元々の実装だとカウント数にずれがでたので、 @comment.reload を追記したら解決した。

comment_likes.controller.erb

Ruby

1class CommentLikesController < ApplicationController 2 before_action :require_user_logged_in 3 before_action :set_comment 4 5 def create 6 unless current_user.comment_liking?(@comment) 7 @comment_like = CommentLike.create(user_id: current_user.id, comment_id: params[:comment_id]) 8 @comment.reload 9 end 10 end 11 12 def destroy 13 comment_like = CommentLike.find_by(user_id: current_user.id, comment_id: params[:comment_id]) 14 if current_user.comment_liking?(@comment) 15 comment_like.destroy 16 @comment.reload 17 end 18 end 19 20 private 21 22 def set_comment 23 @comment = Comment.find(params[:comment_id]) 24 end 25end

長い戦いたっだ。。。

投稿2018/05/02 23:43

hrmths

総合スコア17

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問