Home>

The'Unfollow'button does not work without an error. 'Folo-' is done and the number of followers is also counted.
All index card items are left unfollowed because they cannot be unfollowed.

As a procedure I thought about
· The destroy action cannot find data in the column (user_id, community_id),

・ When you follow, "followed user id: user_id" and "followed community id: community_id" will be displayed.

Should be saved in "Intermediate table: follows table",

I wondered if there was no way to destroy because the data related to follow was not saved in the DB, but I don't know what to do with that.

Please point out something.

class FollowsController</pre>
<p><br /><index.html.erb></p>
<pre><code>      <% @ communities.each do | community |%>                 <% = link_to community_path (community) do%>                                                 <% = attachment_image_tag community,: intro_image, fallback: "no-image.jpg", class: "rounded-top"%>                                                    <% = community.title%>                <% = community.introduction%>                                <% if current_user.present?&&current_user.following? (Community)%>                <% = link_to'Unfollow', community_follows_path (community), method:: destroy%>              <% else%>                <% = link_to'follow', community_follows_path (community), method:: post%>              <% end%><% = community.follows.count%>                       <% end%>             <% end%>

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/applicationCommunity 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)
  • Answer # 1

    The method of link_to is destroy, but I think it is delete.

    #: destroy
    <% = link_to'Unfollow', community_follows_path (community), method:: destroy%>
    Change to #: delete
    <% = link_to'Unfollow', community_follows_path (community), method:: delete%>