フォロー機能を追加したのですが、Followの申請でエラーが出てしまいます。
しかしながらUnfollowする際には、エラーなく正常に機能しています。
Error
1NoMethodError in FriendshipsController#create 2undefined method `user_id' for nil:NilClass 3>**active_friendships.create(follower_id: @current.user_id, followed_id: user.id)**
Ruby
1<% if @current_user != @user %> 2 <% if @current_user.following?(@user) %> 3 <%= button_to "Unfollow", friendship_path(user_id: @user_id), method: :delete, :class => "btn" %> 4 <% else %> 5 <%= button_to "Follow", friendships_path(user_id: @user_id), method: :post, :class => "btn" %> 6 <% end %> 7<% end %>
Controller
1class FriendshipsController < ApplicationController 2 before_action :authenticate_user 3 before_action :find_user 4 5 def create 6 @current_user.follow(@user) 7 redirect_back(fallback_location: "something") 8 end 9 def destroy 10 @current_user.unfollow(@user) 11 redirect_back(fallback_location: "something") 12 end 13 def find_user 14 @user = User.find_by(id: params[:id]) 15 end 16end
Model
1 def follow(user) 2 active_friendships.create(follower_id: @current.user_id, followed_id: user.id) 3 end 4 5 def unfollow(user) 6 active_friendships.find_by(followed_id: user.id).destroy 7 end 8 9 def following?(user) 10 following.include?(user) 11 end
エラーではuser_idの値が正しく取れていないようですが、
unfollow時では正常し、friendshipのdestroyが上手くできてます。
恐れ入りますが、お教え頂ければ幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。