いいね機能を参考にし中身は一緒でフォロー機能を作成したのですが、create.js.erb ,destroy.js.erb のコードに関わらず動作することに気づきコメントアウトしてみたところ、やはり一見フォロー機能は動作しているように見えます。
要するに非同期処理があたかも remote true だけで動いているということです。
(※立て続けの質問になるのですが、フォローボタンをおしたとき出現するフォロー解除ボタンが画像のように下に一段ずれてしまっています。)
ご指摘お願いします。
<フォローした時(create)のログ>
Started POST "/communities/1/follows" for ::1 at 2020-12-14 10:51:45 +0900 Processing by FollowsController#create as JS Parameters: {"community_id"=>"1"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' (0.1ms) begin transaction ↳ app/controllers/follows_controller.rb:5:in `create' Community Load (0.1ms) SELECT "communities".* FROM "communities" WHERE "communities"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."community_id" = ? AND "follows"."user_id" = ? LIMIT ? [["community_id", 1], ["user_id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' Follow Create (0.5ms) INSERT INTO "follows" ("created_at", "updated_at", "user_id", "community_id") VALUES (?, ?, ?, ?) [["created_at", "2020-12-14 01:51:46.010978"], ["updated_at", "2020-12-14 01:51:46.010978"], ["user_id", 1], ["community_id", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' (1.5ms) commit transaction ↳ app/controllers/follows_controller.rb:5:in `create' Redirected to http://localhost:3000/ Completed 200 OK in 14ms (ActiveRecord: 2.5ms | Allocations: 6418) Started GET "/" for ::1 at 2020-12-14 10:51:46 +0900 Processing by CommunitiesController#index as HTML Rendering communities/index.html.erb within layouts/application Community Load (0.2ms) SELECT "communities".* FROM "communities" ↳ app/views/communities/index.html.erb:3 User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/communities/index.html.erb:16 Follow Exists? (0.3ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 1], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 1]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 2], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 2]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.1ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 3], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 3]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.2ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 4], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.3ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 4]] ↳ app/views/communities/index.html.erb:25 Follow Exists? (0.3ms) SELECT 1 AS one FROM "follows" WHERE "follows"."user_id" = ? AND "follows"."community_id" = ? LIMIT ? [["user_id", 1], ["community_id", 5], ["LIMIT", 1]] ↳ app/models/user.rb:11:in `following?' (0.1ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."community_id" = ? [["community_id", 5]] ↳ app/views/communities/index.html.erb:25 Rendered communities/index.html.erb within layouts/application (Duration: 21.8ms | Allocations: 11134) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 32ms (Views: 28.6ms | ActiveRecord: 2.5ms | Allocations: 19319)
<index.htnl.erb>
<div class="container"> <div class="row"> <% @communities.each do |community| %> <div class="col"> <%= link_to community_path(community) do %> <div class="card" style="width: 14rem;"> <div class="card-image"> <%= attachment_image_tag community, :intro_image, fallback:"no-image.jpg", class:"rounded-top" %> </div> <div class="card-body"> <%= community.title %> <%= community.introduction%> </div> <% if current_user.present? && current_user.following?(community) %> <%= link_to community_follows_path(community.id), method: :delete, remote: true do %> <button type="button" class="btn btn-primary btn-sm">フォロー解除</button> <%end%> <% else %> <%= link_to community_follows_path(community.id), method: :post, remote: true do %> <button type="button" class="btn btn-primary btn-sm">フォロー</button> <%end%> <%end%> <%= community.follows.count %> </div> <%end%> </div> <%end%> </div> </div>
<follows.controller.rb>
class FollowsController < ApplicationController def create @follow = current_user.follows.create(community_id:params[:community_id]) redirect_back(fallback_location: root_path) end def destroy @community = Community.find(params[:community_id]) @follow = current_user.follows.find_by(community_id: @community.id) @follow.destroy redirect_back(fallback_location: root_path) end end
<create.js.erb>
// $("<%= @id_community %>").html('<%= escape_javascript(render("follows/follow", community: @community))%>');
<destroy.js.erb
// $("<%= @id_community %>").html('<%= escape_javascript(render("follows/follow", community: @community))%>');
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。