'フォローを外す'ボタンがエラーの発生なしに機能しません。'フォロ-'はできてフォロー数もカウントされています。
フォローを外すことができないので、indexのcardアイテムがすべてフォローされっぱなしになっています。
考えた手順として、
・destroyアクションがカラム(user_id,community_id)からデータを見つけられていない,
・フォローをすると、「フォローしたユーザーのid:user_id」と「フォローされたコミュニティのid:community_id」が
「中間テーブル:followsテーブル」に保存されるはず,
followに関するデータがDBに保存されていないせいでdestroyのしようがないのかなと思いましたが、だからと言ってどうすれば良いのかもわからずじまいです。
何かご指摘願います。
<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
<index.html.erb>
<div class="container"> <div class="row"> <% @communities.each do |community| %> <div class="col-3"> <%= 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), method: :destroy %> <% else %> <%= link_to 'フォロー', community_follows_path(community), method: :post %> <%end%> <%= community.follows.count %> </div> <%end%> </div> <%end%> </div> </div>
<'フォロー'したときのトレース>
Started POST "/communities/4/follows" for ::1 at 2020-12-11 14:52:45 +0900 Processing by FollowsController#create as HTML Parameters: {"authenticity_token"=>"sguO8y1Evt0IGVqh44WM9EJsYaqwJ7tY36REpfoHlUCyiqdBMWGkMMin8157+m3MtmV5pfzN6LlggY5kmDigSg==", "community_id"=>"4"} 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", 4], ["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", 4], ["user_id", 1], ["LIMIT", 1]] ↳ app/controllers/follows_controller.rb:5:in `create' Follow Create (2.9ms) INSERT INTO "follows" ("created_at", "updated_at", "user_id", "community_id") VALUES (?, ?, ?, ?) [["created_at", "2020-12-11 05:52:45.484543"], ["updated_at", "2020-12-11 05:52:45.484543"], ["user_id", 1], ["community_id", 4]] ↳ app/controllers/follows_controller.rb:5:in `create' (0.7ms) commit transaction ↳ app/controllers/follows_controller.rb:5:in `create' Redirected to http://localhost:3000/communities.4 Completed 302 Found in 13ms (ActiveRecord: 4.1ms | Allocations: 6389) Started GET "/communities.4" for ::1 at 2020-12-11 14:52:45 +0900 Processing by CommunitiesController#index as Rendering communities/index.html.erb within layouts/application Community Load (0.1ms) SELECT "communities".* FROM "communities" ↳ app/views/communities/index.html.erb:3 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/communities/index.html.erb:14 Follow Exists? (0.2ms) 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:19 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:19 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:19 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:19 Rendered communities/index.html.erb within layouts/application (Duration: 15.8ms | Allocations: 9268) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 33ms (Views: 30.5ms | ActiveRecord: 1.5ms | Allocations: 16698)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/12 02:51
2020/12/12 02:53
2020/12/12 02:55