前提・実現したいこと
現在SNSのようなアプリのいいね機能を実装中です。
ボタンになっている部分をクリックするとデータベースにはしっかりと記録が残り、リロードをすれば読み込まれます。
・非同期になっていない
・ターミナルにはステータス200、エラーメッセージなし
と言った状態です
ターミナル
Started POST "/posts/1/likes" for ::1 at 2020-12-10 09:27:06 +0900 Processing by LikesController#create as JS Parameters: {"authenticity_token"=>"gLf2VbgAMI4UilZjwT13haZKK0LDa+wbfrBQ01UVsYyGDhEp7C/b4CKzqM3oFWv+ThkVcXMsN+FPFIUSlUwNbA==", "post_id"=>"1"} Post Load (0.5ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 1 LIMIT 1 ↳ app/controllers/likes_controller.rb:21:in `set_post' User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/likes_controller.rb:6:in `create' (0.2ms) BEGIN ↳ app/controllers/likes_controller.rb:6:in `create' Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 1 LIMIT 1 ↳ app/controllers/likes_controller.rb:6:in `create' User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 ↳ app/controllers/likes_controller.rb:6:in `create' Like Create (0.4ms) INSERT INTO `likes` (`user_id`, `post_id`, `created_at`, `updated_at`) VALUES (1, 1, '2020-12-10 00:27:06.440564', '2020-12-10 00:27:06.440564') ↳ app/controllers/likes_controller.rb:6:in `create' Post Update All (0.5ms) UPDATE `posts` SET `posts`.`likes_count` = COALESCE(`posts`.`likes_count`, 0) + 1 WHERE `posts`.`id` = 1 ↳ app/controllers/likes_controller.rb:6:in `create' (1.3ms) COMMIT ↳ app/controllers/likes_controller.rb:6:in `create' Rendering likes/create.js.erb Like Load (0.5ms) SELECT `likes`.* FROM `likes` WHERE `likes`.`post_id` = 1 AND `likes`.`user_id` = 1 LIMIT 1 ↳ app/models/post.rb:6:in `like_user' Rendered likes/_like.html.erb (Duration: 5.1ms | Allocations: 1379) Rendered likes/create.js.erb (Duration: 7.5ms | Allocations: 1731) Completed 200 OK in 36ms (Views: 11.4ms | ActiveRecord: 5.0ms | Allocations: 10875)
該当のソースコード
Ruby
1_like.html.erb 2 3<% if user_signed_in? %> 4 <% if post.like_user(current_user.id) %> 5 <%= button_to post_like_path(like, post_id: post.id), method: :delete, id: "but-like-button", remote: true do %> 6 <i class="fas fa-star"></i> 7 <span> 8 <%= post.likes_count %> 9 </span> 10 <% end %> 11 <% else %> 12 <%= button_to post_likes_path(post), id: "like-button", remote: true do %> 13 <i class="far fa-star"></i> 14 <span> 15 <%= post.likes_count %> 16 </span> 17 <% end %> 18 <% end %> 19 <% else %> 20 <i class="far fa-star"></i> 21 <span> 22 <%= post.likes_count %> 23 </span> 24<% end %> 25
Ruby
1create.js.erb 2 3$("#like-button-#{@post.id}").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like, post: @post}) %>") 4
Ruby
1destroy.js.erb 2 3$("#like-button-#{@post.id}").html("<%= j(render partial: 'like', locals: { posts: @posts, likes: @likes, like: @like, post: @post}) %>") 4 5
Ruby
1likes_contoroller 2 3 before_action :set_post 4 5 def create 6 @like = Like.create(user_id: current_user.id, post_id: params[:post_id]) 7 @likes = Like.where(post_id: params[:post_id]) 8 @posts = Post.all 9 10 end 11 12 def destroy 13 like = Like.find_by(user_id: current_user.id, post_id: params[:post_id]) 14 like.destroy 15 @likes = Like.where(post_id: params[:post_id]) 16 @posts = Post.all 17 end 18 19 private 20 def set_post 21 @post = Post.find(params[:post_id]) 22 end 23 24end 25
Ruby
1like.rb 2 3class Like < ActiveRecord::Base 4 belongs_to :post, counter_cache: :likes_count 5 belongs_to :user 6end 7 8
Ruby
1routes.rb 2 3 devise_for :users 4 root to: 'posts#index' 5 6 resources :users, only: :show 7 8 resources :posts do 9 resources :likes, only: [:create, :destroy] 10 end 11 12end
上記以外に気になる点などがありましたらコメントいただけたら幸いです。
試したこと
https://qiita.com/YuitoSato/items/94913d6a349a530b2ea2
こちらの記事を参考に実装を進めています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/09 16:25 編集