質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

2197閲覧

【Ruby on Rails】フォロー機能の非同期が一部反映されない。

is02

総合スコア17

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/02/07 03:09

編集2020/02/09 14:29

前提・実現したいこと

フォロー機能の非同期が一部出来ない。
イメージ説明

上記の画像のように、画像を投稿した最新のものには、非同期フォローが出来ているんですけど、
過去に投稿したものには非同期で反映されません。(リロードすると反映されます)

発生している問題・エラーメッセージ

検証ツールのconsoleにて、非同期が出来ていない部分を確認してみると、
500 (Internal Server Error)というエラーが出ていました。

該当のソースコード

####モデル
user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user # 2~8文字以内で名前が入っているかの確認 validates :name, presence: true, length: { in: 2..8 }, uniqueness: true # 自己紹介 100文字以内 validates :introduction, length: { maximum: 100 } # 可能なコスプレ 100文字以内 validates :like_cos, length: { maximum: 100 } # refile定義 attachment :profile_image # フォロー機能のメソッド def follow(other_user) unless self == other_user # フォローしようとしている人が自分自身ではないか # フォローしようとしている人が自分以外ならフォローする self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship # フォローしていたらアンフォローする end def following?(other_user) self.followings.include?(other_user) # other_userが含まれていたらtrueを返す end end

relationship.rb

class Relationship < ApplicationRecord belongs_to :user # userモデルを参照する belongs_to :follow, class_name: 'User' validates :user_id, presence: true validates :follow_id, presence: true end

post_image.rb

class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy # refile定義 attachment :real_image attachment :cosplay_image validates :real_image, presence: true validates :cosplay_image, presence: true # 投稿を降順に並び替えし、最新のものを上にくるようにする default_scope -> { order(created_at: :asc) } end

####コントローラー
users_controller.rb

class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to user_path(@user.id) else render 'edit' end end def following @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end def followers @user = User.find(params[:id]) @users = @user.followers render 'show_follower' end private def user_params params.require(:user).permit(:name, :profile_image, :introduction, :like_cos) end end

relationships_controller.rb

class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) following.save respond_to do |format| format.html { redirect_to @user } format.js end end def destroy following = current_user.unfollow(@user) following.destroy respond_to do |format| format.html { redirect_to @user } format.js end end private def set_user @user = User.find(params[:follow_id]) end end

post_images_controller.rb

class PostImagesController < ApplicationController def new @post_image = PostImage.new end # 投稿データの保存 def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id if @post_image.save redirect_to post_images_path else render 'new' end end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private # 投稿データのストロングパラメータ def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end

####ビュー
relationships/_follow_button.rb

<% unless current_user == user %> <div id="follow_form"> <% if current_user.following?(user) %> <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォロー中' %> <% end %> <% else %> <%= form_for(current_user.relationships.build, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォローする' %> <% end %> <% end %> </div> <% end %>

create.js.erb

$("<%= '#follow_form' %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');

destroy.js.erb

$("<%= '#follow_form' %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');

post_images/index.html.erb

<% @post_images.each do |post_image| %> <%= render 'relationships/follow_button', user: post_image.user %> <% end %>

補足情報(FW/ツールのバージョンなど)

ruby 2.5.7p206
Rails 5.2.4.1

###追記

####_follow_button.html.erb

<% unless current_user == user %> <div id="follow_form_<%= user.id %>"> <% if current_user.following?(user) %> <%= form_for(current_user.relationships.find_by(follow_id: user.id), html: { method: :delete }, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォロー中' %> <% end %> <% else %> <%= form_for(current_user.relationships.build, remote: true) do |f| %> <%= hidden_field_tag :follow_id, user.id %> <%= f.submit 'フォローする' %> <% end %> <% end %> </div> <% end %>

####create.js.erb, destroy.js.erb

$("<%= @userid %>").html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');

####relationships_controller.rb

class RelationshipsController < ApplicationController before_action :set_user def create following = current_user.follow(@user) following.save respond_to do |format| format.html { redirect_to @user } format.js end end def destroy following = current_user.unfollow(@user) following.destroy if following respond_to do |format| format.html { redirect_to @user } format.js end end private def set_user @user = User.find(params[:follow_id]) @userid = "#follow_form_#{@user.id}" end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/02/07 09:14

500エラーが出たところのlogを載せてください
is02

2020/02/07 09:18

コメントありがとうございます。載せました、よろしくお願いします。
winterboum

2020/02/07 09:19

それではなく、logを見たいです
is02

2020/02/07 09:23

すみません、ターミナル上のログ載せました。
is02

2020/02/07 09:25

非同期が出来ている箇所のログに赤文字が出ていたので、念のために非同期が出来ている箇所と出来ていない箇所のログを載せました。
is02

2020/02/07 09:29

また、非同期が出来ていない箇所のフォローボタンを押すと、非同期が出来ている箇所が切り替わります。
winterboum

2020/02/07 10:31

textで載せてほしいなぁ。 読みにくいしコピペできないんで回答への引用も面倒
winterboum

2020/02/07 10:32

「500 (Internal Server Error)というエラーが出ていました。」というのが見当たりません
is02

2020/02/07 14:23 編集

申し訳ありません。 ログ中にCannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255という文言があり、エラーのような気がしたので、config/application.rb内に「config.web_console.whitelisted_ips = '10.0.2.2'」というものを追記しました。(それ以降でなくなりました。) また、検証ツールでserver error 500の部分を見ていると、 NoMethodError in RelationshipsController#destroy undefined method `destroy' for nil:NilClass Extracted source (around line #15): #13 def destroy #14 following = current_user.unfollow(@user) *15 following.destroy #16 respond_to do |format| #17 format.html { redirect_to @user } #18 format.js Extracted source (around line #6): #4 module BasicImplicitRender # :nodoc: #5 def send_action(method, *args) *6 super.tap { default_render unless performed? } #7 end #8 #9 def default_render(*args) Extracted source (around line #194): #192 # which is *not* necessarily the same as the action name. #193 def process_action(method_name, *args) *194 send_action(method_name, *args) #195 end #196 #197 # Actually call the method associated with the action. Override というエラーが出ているのが分かりました。 following.destroyのdestroyの中身が定義されていないということで、確認しているのですが、どう直せばいいか分かりません。よろしくお願いします。
is02

2020/02/07 14:28

【非同期出来ている箇所のログ】 「フォローを外すときのログ」 Started DELETE "/relationships/43" for 10.0.2.2 at 2020-02-07 14:27:10 +0000 Processing by RelationshipsController#destroy as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォロー中", "id"=>"43"} User Load (5.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (5.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (6.7ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:37 (0.3ms) begin transaction ? app/models/user.rb:38 Relationship Destroy (30.3ms) DELETE FROM "relationships" WHERE "relationships"."id" = ? [["id", 43]] ? app/models/user.rb:38 (30.9ms) commit transaction ? app/models/user.rb:38 (0.2ms) begin transaction ? app/controllers/relationships_controller.rb:15 (0.2ms) commit transaction ? app/controllers/relationships_controller.rb:15 Rendering relationships/destroy.js.erb User Exists (5.4ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Rendered relationships/_follow_button.html.erb (19.3ms) Rendered relationships/destroy.js.erb (190.5ms) Completed 200 OK in 960ms (Views: 659.6ms | ActiveRecord: 84.8ms) 「フォローするときのログ」 Started POST "/relationships" for 10.0.2.2 at 2020-02-07 14:28:18 +0000 (6.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Processing by RelationshipsController#create as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォローする"} User Load (6.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (3.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (4.1ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 (0.1ms) begin transaction ? app/models/user.rb:32 User Load (4.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 Relationship Create (18.6ms) INSERT INTO "relationships" ("user_id", "follow_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 4], ["follow_id", 5], ["created_at", "2020-02-07 14:28:18.906367"], ["updated_at", "2020-02-07 14:28:18.906367"]] ? app/models/user.rb:32 (18.5ms) commit transaction ? app/models/user.rb:32 (0.1ms) begin transaction ? app/controllers/relationships_controller.rb:6 (0.1ms) commit transaction ? app/controllers/relationships_controller.rb:6 Rendering relationships/create.js.erb User Exists (3.7ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Relationship Load (4.6ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/views/relationships/_follow_button.html.erb:4 Rendered relationships/_follow_button.html.erb (34.0ms) Rendered relationships/create.js.erb (145.7ms) Completed 200 OK in 874ms (Views: 450.4ms | ActiveRecord: 107.9ms)
is02

2020/02/07 14:30

【非同期出来ていない箇所のログ】 「フォローするときのログ」 Started POST "/relationships" for 10.0.2.2 at 2020-02-07 14:29:08 +0000 (6.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Processing by RelationshipsController#create as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォローする"} User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (3.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (5.3ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 (0.1ms) begin transaction ? app/controllers/relationships_controller.rb:6 User Load (3.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:6 (0.2ms) commit transaction ? app/controllers/relationships_controller.rb:6 Rendering relationships/create.js.erb User Exists (2.8ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 CACHE Relationship Load (0.0ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/views/relationships/_follow_button.html.erb:4 Rendered relationships/_follow_button.html.erb (23.4ms) Rendered relationships/create.js.erb (91.1ms) Completed 200 OK in 551ms (Views: 267.8ms | ActiveRecord: 50.8ms) 非同期出来ていない箇所は、フォロー外すときのログが出ませんでした。
winterboum

2020/02/07 21:07

14 following = current_user.unfollow(@user) 15 following.destroy 14で該当するunfollowがなかったのでnilが入りそれで15となりました。 following.destroy if follwing とでもしてください。 そもそもdestroyすべき対象が居ないの削除ボタンが機能するのがおかしいですが
guest

回答2

0

ベストアンサー

同一ページ内にid="follow_form"の要素が複数個存在しているのが原因です。
jsの$("<%= '#follow_form' %>")は最初に見つかった#follow_form要素を返すので
最新のものしかフォローが反映されないと思ったのもそのためでしょう。
同一ページ内で要素のIDが重複しないようにしましょう。

ex.

erb

1<%# relationships/_follow_button.html.erb %> 2 <div id='follow_form_<%= user.id %>>

erb

1<%# create.js.erb %> 2<%# destroy.js.erb %> 3$('<%= "#follow_form_#{@user.id}" %>').html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');

erbの書き方に慣れてないのでもしかしたら書き方違うかも。

追記

もしかして...

erb

1<%# relationships/_follow_button.html.erb %> 2 <div class='follow_form_<%= user.id %>>

erb

1<%# create.js.erb %> 2<%# destroy.js.erb %> 3$('<%= ".follow_form_#{@user.id}" %>').html('<%= escape_javascript(render("relationships/follow_button", user: @user )) %>');

投稿2020/02/09 05:57

編集2020/02/09 14:50
Mugheart

総合スコア2340

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

is02

2020/02/09 14:31

お二方、回答ありがとうございます。 仰った書き方でやってみて反応しなかったので、追記に書いたやり方で書いてみたのですが、投稿が最新のものしか反応してくれません。アドバイス頂けると幸いです。
Mugheart

2020/02/09 14:43

回答に書いたのは @userid ではなく @user.id です。 あと、適当に user.id としてますが、この @post_images.each do ... end で同じユーザーの投稿が複数表示される場合同じくIDが重複してしまうのでその場合はpost_imageのidでIDをつけるなり重複しないようにしてくださいね。
Mugheart

2020/02/09 14:51 編集

もしかしてと思ったので一応書いておきますが、回答にも書いた通り、ID指定では要素は最初に見つかった1つしか取得できないので、もし複数の要素を一気に変更したいのであればclassを使ってください。classはページ内で重複してもOKです。同じclassはまとめて取得できてまとめて変更できます。 回答にも追記しておきました。
is02

2020/02/09 14:54

ありがとうございます!なるほど、id指定だと1つしか要素を取得できないんですね。クラス指定でやったら出来ました! Mugheartさん、winterboumさん、長らくお付き合い頂き本当にありがとうございました。 今後ともよろしくお願いします。
guest

0

最初の書き込みで勘違いしたのは、以下の現象からです。

def create following = current_user.follow(@user) following.save

「失敗」のlogはこの部分でINSERTされていません。つまり、既にそのデータは有ったと言うことを意味しています。にも関わらず フォローするボタンが出ていたのはおかしいですね。
ここを詰める必要がありそうです。
User、Relationship の状態を一覧し、
viewでのボタンの出方を確認し、
ボタンを押した後のRelationship の状態を確認し
viewの出方を確認する
というのを見てみたい


いろいろ問題がありそうですが、まずここがおかしいのでは?
def following?(other_user) self.followings.include?(other_user) # other_userが含まれていたらtrueを返す end
<% if current_user.following?(user) %>

current_user : loginしているユーザ
user : 画面に表示されているユーザ
ですから、
この if 文は userとcurrent_user が入れ替わっているようです
修正
勘違い。follow の方向を逆に見てました、ごめんなさい。
も少し考えて回答し直します

投稿2020/02/07 21:36

編集2020/02/08 08:46
winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

is02

2020/02/08 09:30 編集

いえ、ご丁寧に回答して頂いて感謝の極みです。 ・ボタンを押した後のRelationship の状態を確認 ・User、Relationship の状態を一覧する →どのページを表示させればよろしいでしょうか。 ・viewでのボタンの出方を確認 →htmlで見た時のボタンが「フォローする」か「フォロー中」のどちらになっているか確認するということでしょうか。 理解力が乏しくて申し訳ありませんが、よろしくお願いします。
winterboum

2020/02/08 11:24

1) rails c にて、 Relationship.pluck(:user_id,:follow_id) 2) そのなかで片方向だけのフォロー関係の一組を選ぶ 3) そのuserでloginし、followユーザの画面を開く 4) どちらのボタンか記録 5)そのボタンを押す 6) 結果ボタンので具合を確認 7) Relationship.pluck(:user_id,:follow_id)
is02

2020/02/08 14:55

1) SELECT "relationships"."user_id", "relationships"."follow_id" FROM "relationships" => [[5, 4]] 2) test1はtest2をフォローしていない test2がtest1をフォローしている 3) test1でログイン 4) 「フォローする」ボタンになっている 5) ①投稿が最新のボタンを押す →「フォローする」が「フォロー中」に変わる ②投稿が最新よりも古いボタンを押す →変わらない 6) ①でボタンを押したときのログ 「フォローする」→「フォロー中」 Started POST "/relationships" for 10.0.2.2 at 2020-02-08 14:44:06 +0000 Processing by RelationshipsController#create as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォローする"} User Load (4.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (4.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (5.7ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 (0.2ms) begin transaction ? app/models/user.rb:32 User Load (5.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 Relationship Create (25.8ms) INSERT INTO "relationships" ("user_id", "follow_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 4], ["follow_id", 5], ["created_at", "2020-02-08 14:44:06.818152"], ["updated_at", "2020-02-08 14:44:06.818152"]] ? app/models/user.rb:32 (33.4ms) commit transaction ? app/models/user.rb:32 (0.3ms) begin transaction ? app/controllers/relationships_controller.rb:6 (0.2ms) commit transaction ? app/controllers/relationships_controller.rb:6 Rendering relationships/create.js.erb User Exists (4.0ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Relationship Load (3.5ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/views/relationships/_follow_button.html.erb:4 Rendered relationships/_follow_button.html.erb (18.0ms) Rendered relationships/create.js.erb (131.8ms) Completed 200 OK in 841ms (Views: 528.9ms | ActiveRecord: 87.3ms) 「フォロー中」→「フォローする」 Started DELETE "/relationships/49" for 10.0.2.2 at 2020-02-08 14:46:33 +0000 (5.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Processing by RelationshipsController#destroy as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォロー中", "id"=>"49"} User Load (5.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (4.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (5.8ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:37 (0.2ms) begin transaction ? app/models/user.rb:38 Relationship Destroy (24.9ms) DELETE FROM "relationships" WHERE "relationships"."id" = ? [["id", 49]] ? app/models/user.rb:38 (34.0ms) commit transaction ? app/models/user.rb:38 (0.4ms) begin transaction ? app/controllers/relationships_controller.rb:15 (0.2ms) commit transaction ? app/controllers/relationships_controller.rb:15 Rendering relationships/destroy.js.erb User Exists (3.9ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Rendered relationships/_follow_button.html.erb (27.8ms) Rendered relationships/destroy.js.erb (166.7ms) Completed 200 OK in 1060ms (Views: 571.1ms | ActiveRecord: 124.1ms)
is02

2020/02/09 05:19 編集

②でボタンを押したときのログ 「フォローする」→「フォロー中」 Started POST "/relationships" for 10.0.2.2 at 2020-02-08 14:51:07 +0000 Processing by RelationshipsController#create as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォローする"} User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (1.5ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 (0.1ms) begin transaction ? app/models/user.rb:32 User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/models/user.rb:32 Relationship Create (10.4ms) INSERT INTO "relationships" ("user_id", "follow_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 4], ["follow_id", 5], ["created_at", "2020-02-08 14:51:07.228535"], ["updated_at", "2020-02-08 14:51:07.228535"]] ? app/models/user.rb:32 (9.7ms) commit transaction ? app/models/user.rb:32 (0.1ms) begin transaction ? app/controllers/relationships_controller.rb:6 (0.1ms) commit transaction ? app/controllers/relationships_controller.rb:6 Rendering relationships/create.js.erb User Exists (1.2ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Relationship Load (1.4ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/views/relationships/_follow_button.html.erb:4 Rendered relationships/_follow_button.html.erb (7.7ms) Rendered relationships/create.js.erb (38.7ms) Completed 200 OK in 237ms (Views: 149.0ms | ActiveRecord: 29.1ms)
is02

2020/02/09 05:19 編集

「フォロー中」→「フォローする」 Started DELETE "/relationships/50" for 10.0.2.2 at 2020-02-08 14:49:14 +0000 Processing by RelationshipsController#destroy as JS Parameters: {"utf8"=>"?", "follow_id"=>"5", "commit"=>"フォロー中", "id"=>"50"} User Load (1.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 1]] ? /var/lib/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] ? app/controllers/relationships_controller.rb:25 Relationship Load (1.8ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."user_id" = ? AND "relationships"."follow_id" = ? LIMIT ? [["user_id", 4], ["follow_id", 5], ["LIMIT", 1]] ? app/models/user.rb:37 (0.1ms) begin transaction ? app/models/user.rb:38 Relationship Destroy (12.2ms) DELETE FROM "relationships" WHERE "relationships"."id" = ? [["id", 50]] ? app/models/user.rb:38 (9.1ms) commit transaction ? app/models/user.rb:38 (0.1ms) begin transaction ? app/controllers/relationships_controller.rb:15 (0.1ms) commit transaction ? app/controllers/relationships_controller.rb:15 Rendering relationships/destroy.js.erb User Exists (1.8ms) SELECT 1 AS one FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follow_id" WHERE "relationships"."user_id" = ? AND "users"."id" = ? LIMIT ? [["user_id", 4], ["id", 5], ["LIMIT", 1]] ? app/models/user.rb:42 Rendered relationships/_follow_button.html.erb (7.3ms) Rendered relationships/destroy.js.erb (39.8ms) Completed 200 OK in 192ms (Views: 117.6ms | ActiveRecord: 28.4ms) 7) SELECT "relationships"."user_id", "relationships"."follow_id" FROM "relationships" => [[4, 5], [5, 4]]
winterboum

2020/02/08 21:41

_follow_button.rb を呼んでいるviewを見せてください
is02

2020/02/09 00:46

post_images/index.html.erb <div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿する', new_post_image_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="post_images_index_wrapper"> <% @post_images.each do |post_image| %> <div class="index_box"> <div class="post_images_index_user"> <ul> <li> <%= link_to user_path(post_image.user) do %> <%= attachment_image_tag post_image.user, :profile_image, fallback: "no_image.jpg" %> <% end %> </li> <li> <p><%= link_to "#{post_image.user.name}", user_path(post_image.user) %></p> </li> <li> <%= render 'relationships/follow_button', user: post_image.user %> </li> </ul> </div> <div class="post_images_index_title"> <div class="image_title"> <h2>Real</h2> </div> <div class="image_title"> <h2>Cosplay</h2> </div> </div> <div class="post_images_box"> <div class="post_image"> <%= link_to post_image_path(post_image) do %> <%= attachment_image_tag post_image, :real_image %> <% end %> </div> <div class="post_image"> <%= link_to post_image_path(post_image) do %> <%= attachment_image_tag post_image, :cosplay_image %> <% end %> </div> </div> <div class="image_name"> <%= post_image.real_image_name %> </div> <div class="image_name"> <%= post_image.cosplay_image_name %> </div> <div class="favorites_area"> <div class="favorite_area"> <%= render partial: 'post_images/post_images', locals: { post_image: post_image } %> </div> <div class="favorite_area"> <%= render partial: 'post_images/cosplay_post_images', locals: { post_image: post_image } %> </div> </div> <div class="image_caption"> <ul> <li> <%= link_to user_path(post_image.id) do %> <%= attachment_image_tag post_image.user, :profile_image, fallback: "no_image.jpg" %> <% end %> </li> <li> <p><%= link_to "#{post_image.user.name}", user_path(post_image.id) %> </p> </li> <li> <p><%= link_to "#{post_image.post_comments.count}件のコメント", post_image_path(post_image.id) %></p> </li> </ul> <span class="caption"><%= post_image.caption %></span> </div> <%= @a %> </div> <% end %> <%= paginate @post_images, class: "pagenate" %> </div> 載せました、よろしくお願いします。
winterboum

2020/02/09 02:17

読みにくい、、、、 質問の方に<code>で載せて。。。 とりあえず読みます
winterboum

2020/02/09 02:28

それと2020/02/08 23:55にコメントした中にあるlog編集しなおしてください。 1)不要な部分があるので削除 Started GET から始まり次のStartの直前まで 2)POST,DELETE の始まりの説明が例えば 「フォロー中」→「フォローする」 なのを、以下を追加してください ・誰がloginし、誰のにしようとしているのか(user.idこみで) ・成功したのか失敗したノア
Mugheart

2020/02/09 04:21

#follow_form が index ページに複数あるのが原因ですよ。 jsで $("<%= '#follow_form' %>") としていますが、これは最初に見つかった #follow_form 要素を返すので最新のものしかフォローが反映されないと思ったのもそのためでしょう。 それそれの要素のIDがページ内で重複しないようにしましょう。
winterboum

2020/02/09 05:45

そか。 Mugheartさん、それで回答書いてください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問