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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

1796閲覧

フォロー機能でエラー(申請時)

tomtom1

総合スコア168

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2018/11/22 12:40

フォロー機能を追加したのですが、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が上手くできてます。

恐れ入りますが、お教え頂ければ幸いです。

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

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

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

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

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

guest

回答2

0

自己解決

def find_user
@user = User.find_by(id: params[:id])
end
を、@user = User.find_by(id: params[:user_id])
に変更したら正常に動きました。
ありがとうございました。

投稿2018/11/24 03:57

tomtom1

総合スコア168

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

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

0

find_userでuser_idではなくidを取っているので、

friendships_path(user_id: @user_id)

friendships_path(id: @user_id)
とすると解決するような気がします。
(が、なぜunfollowでは動くのかはわかりません。。)

投稿2018/11/22 17:02

mariguranule

総合スコア204

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

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

tomtom1

2018/11/23 02:33

ご回答ありがとうございます。 残念ながら同様のエラーがでてしまいました。 他に考えられる解決方法があれば、お教え頂ければ幸いです。
mariguranule

2018/11/23 02:36

def follow(user) active_friendships.create(follower_id: @current.user_id, followed_id: user.id) end のところ、 @current.user_id ではなく @current_user.id ですね!
tomtom1

2018/11/23 04:01

ご回答ありがとうございます。 active_friendships.create(follower_id: @current_user.id, followed_id: user_id) に変更しましたところ、undefined method `id' for nil:NilClassのエラーがでました。 また、確認のために敢えて、follower_id: @current_user.id, を抜いたところ、 undefined local variable or method `user_id' for #<User:000>のエラーがでます。
tomtom1

2018/11/23 04:40 編集

そしてまた敢えて、下記のように値を入れて確認しましたところ、 active_friendships.create(follower_id: 1, followed_id: 2) 正常にuser_id:1がuser_id:2をフォローすることに成功しました。 そこで下記2つで確認しましたところ、 active_friendships.create(follower_id: @current_user.id, followed_id: 2) active_friendships.create(follower_id: 1, followed_id: user.id) 両方共、undefined method `id' for nil:NilClassのエラーが出現しました。 従って、両方の値の取得がしっかりできていないという現状と考察しています。 Unfollowでは上手くいくところがまた謎が深まります。
mariguranule

2018/11/23 05:03

@current_userではなく@なしのcurrent_userでできる気がします。
tomtom1

2018/11/23 05:21

ありがとうございます。 試しましたが、undefined local variable or method `current_user' のエラーがでます。 また、active_friendships.create(followed_id: 2)で挑戦したところ、 正常にuser_id:1がuser_id:2をフォローできました。 従って、follow_idの値のみ必要としている状況なのですが、 active_friendships.create(followed_id: user_id)ですとやはり、 undefined local variable or method `user_id'のエラーが発生します。 def unfollow(user) active_friendships.find_by(followed_id: user.id).destroy end のアンフォローではしっかしとuser_idの値を取得できているのですが..
tomtom1

2018/11/23 13:54

また、UserControllerに下記の記載がありますが、 エラーの原因となる値の取得を制限している記述なのありましたらご指摘頂ければ幸いです。 before_action :authenticate_user, {only: [:index, :show, :edit, :update]} before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]} before_action :ensure_correct_user, {only: [:edit, :update]}
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問