yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:フォローする際の挙動 #5

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago

フォローボタンのhtml(app/views/users/_follow.html.erb)

<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
  <div><%= hidden_field_tag :followed_id, @user.id %></div>
  <%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>

フォローボタンを押した際にサーバーに送信されるメッセージ

Started POST "/relationships" for 127.0.0.1 at 2018-11-29 14:27:12 +0900
Processing by RelationshipsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"94969Q6WF+7ho+BNWpnNIjrLVvXs8LkMeI/S7gqgJY9abX4p80egdHsWtO/tk2N1/8Ua8DhcnVwQM1+EUHatJQ==", "followed_id"=>"2", "commit"=>"Follow"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:23
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/relationships_controller.rb:5
   (0.0ms)  begin transaction
  ↳ app/models/user.rb:90
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/models/user.rb:90
  Relationship Create (0.5ms)  INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["follower_id", 1], ["followed_id", 2], ["created_at", "2018-11-29 05:27:12.591811"], ["updated_at", "2018-11-29 05:27:12.591811"]]
  ↳ app/models/user.rb:90
   (6.6ms)  commit transaction
  ↳ app/models/user.rb:90
  Rendering relationships/create.js.erb
  Relationship Load (0.2ms)  SELECT  "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = ? AND "relationships"."followed_id" = ? LIMIT ?  [["follower_id", 1], ["followed_id", 2], ["LIMIT", 1]]
  ↳ app/views/users/_unfollow.html.erb:1
  Rendered users/_unfollow.html.erb (1.9ms)
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 2]]
  ↳ app/views/relationships/create.js.erb:2
  Rendered relationships/create.js.erb (5.7ms)
Completed 200 OK in 29ms (Views: 13.3ms | ActiveRecord: 7.9ms)

フォローボタン押した後のアクション(app/controllers/relationships_controller.rb/)

def create
        @user = User.find(params[:followed_id])
        current_user.follow(@user)
        respond_to do |format|
            format.html { redirect_to @user }
            format.js
        end
end
def follow(other_user)
    following << other_user
end
yuyanegishi commented 5 years ago
user1 = User.find_by(email: "example@railstutorial.org")

user1.active_relationships
=> #<ActiveRecord::Associations::CollectionProxy [#<Relationship id: 1, follower_id: 1, followed_id: 3, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 2, follower_id: 1, followed_id: 4, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 3, follower_id: 1, followed_id: 5, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 4, follower_id: 1, followed_id: 6, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 6, follower_id: 1, followed_id: 8, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 7, follower_id: 1, followed_id: 9, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 8, follower_id: 1, followed_id: 10, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 9, follower_id: 1, followed_id: 11, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 10, follower_id: 1, followed_id: 12, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, #<Relationship id: 11, follower_id: 1, followed_id: 13, created_at: "2018-11-22 01:14:29", updated_at: "2018-11-22 01:14:29">, ...]>

user1.active_relationships.build
=> #<Relationship id: nil, follower_id: 1, followed_id: nil, created_at: nil, updated_at: nil>

user1.following
=> #<ActiveRecord::Associations::CollectionProxy [#<User id: 2, name: "Ashly Cronin", email: "example-1@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$uey4g1kJQInnaI9Z4O2fy.WRPpnpbjaLtljBS4MkwXm...", remember_digest: nil, admin: false, activation_digest: "$2a$10$5Alb73UJmDRFcQgmTW8./OvRMbG/H6XX1iEjKoUkUzr...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>, #<User id: 3, name: "Jovany Kiehn", email: "example-2@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$48aqF/P7S8RnS6p0BCxfVu1DyHd1aBYPFPTDtamcfpQ...", remember_digest: nil, admin: false, activation_digest: "$2a$10$JWyZVRz7oJ.70cgC1eo1/eyHRvFbk1NQRBNWXX1DEgN...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>, #<User id: 4, name: "Vena Pagac PhD", email: "example-3@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$P3ao5ofSHGvR9fJOIgeiNe4yNCrKU1owuNJkfS4qFWg...", remember_digest: nil, admin: false, activation_digest: "$2a$10$6ist9IyCE0RbOgGAuH4qWeuuiwHEuGx5poVOHuA/l2I...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>, #<User id: 5, name: "Chesley Dach", email: "example-4@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$3NMzIyMEAdnQGCSyyBaE6OwyiO0y4WHtkpBAmXiAR7F...", remember_digest: nil, admin: false, activation_digest: "$2a$10$JeBbZF/1W1JWW1wUmS0rGe4L52fshphRYSdczF6xqz8...", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>, #<User id: 6, name: "Miss Bethel O'Connell", email: "example-5@railstutorial.org", created_at: "2018-11-22 01:14:16", updated_at: "2018-11-22 01:14:16", password_digest: "$2a$10$4xycyOBBcgaQr.yKjoxbFedOlP9bAgQRGhkTRBp6/.M...", remember_digest: nil, admin: false, activation_digest: "$2a$10$q5cfWkVeuaMpT9RVWcYobOA0nXL/OjVCIOeBg/Q6nL....", activated: true, activated_at: "2018-11-22 01:14:16", reset_digest: nil, reset_sent_at: nil>, #<User id: 8, name: "Neva Douglas", email: "example-7@railstutorial.org", created_at: "2018-11-22 01:14:17", updated_at: "2018-11-22 01:14:17", password_digest: "$2a$10$ak.DumvrT5u/TVR.cDu86O8eiiXPUUCQW3BWYn7O480...", remember_digest: nil, admin: false, activation_digest: "$2a$10$Snii4UZITXUAXg.iji2BRuIMmst5bEpdEIcfinRZtlm...", activated: true, activated_at: "2018-11-22 01:14:17", reset_digest: nil, reset_sent_at: nil>, #<User id: 9, name: "Mr. Enrico Goldner", email: "example-8@railstutorial.org", created_at: "2018-11-22 01:14:17", updated_at: "2018-11-22 01:14:17", password_digest: "$2a$10$Nn15RddYgksoL2OZNYKYKerlKJvSLvEoGQOo/NB6GEW...", remember_digest: nil, admin: false, activation_digest: "$2a$10$xuwcO4SCeLGRFrMn9AvqyeR6TacQdnhiMjmRAhwXbxW...", activated: true, activated_at: "2018-11-22 01:14:17", reset_digest: nil, reset_sent_at: nil>, #<User id: 10, name: "Susana DuBuque", email: "example-9@railstutorial.org", created_at: "2018-11-22 01:14:17", updated_at: "2018-11-22 01:14:17", password_digest: "$2a$10$tGRMgXy1XyDV5BRssODcSOCS2qODgugma9SHEtnq9lN...", remember_digest: nil, admin: false, activation_digest: "$2a$10$0qPECPBmgIBFkI/sZ/UZmedjHYUO4XqUCrTOck8RTYf...", activated: true, activated_at: "2018-11-22 01:14:17", reset_digest: nil, reset_sent_at: nil>, #<User id: 11, name: "Braeden Mosciski", email: "example-10@railstutorial.org", created_at: "2018-11-22 01:14:17", updated_at: "2018-11-22 01:14:17", password_digest: "$2a$10$5KYGK0GtOCCN9BHfWrdd.u9T7z5rj7qOvDxOEb0u/XC...", remember_digest: nil, admin: false, activation_digest: "$2a$10$8DFgq6e9galtHQ0ouhru1OeMTjI2zCGiwzc7ymf2/ZY...", activated: true, activated_at: "2018-11-22 01:14:17", reset_digest: nil, reset_sent_at: nil>, #<User id: 12, name: "Marques Feeney", email: "example-11@railstutorial.org", created_at: "2018-11-22 01:14:17", updated_at: "2018-11-22 01:14:17", password_digest: "$2a$10$ql8UugU4BX/PFWwBD3eYOe/bwl2ayfgOEHCQ3GzWUNx...", remember_digest: nil, admin: false, activation_digest: "$2a$10$ixpC1EGYDdOsJ1W3Bik/RO6B94YyP0e5aQT8ADdIKh6...", activated: true, activated_at: "2018-11-22 01:14:17", reset_digest: nil, reset_sent_at: nil>, ...]>