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

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

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

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

Q&A

解決済

1回答

2106閲覧

ログイン中のユーザを取り出したい

majikadezou

総合スコア34

Ruby on Rails

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

0グッド

0クリップ

投稿2019/12/24 04:28

編集2019/12/24 07:26

フォローしているユーザの投稿を表示する機能を作りたいと思っています。
そこで、アカウントにログインしている、current_user を@userに代入したいと思っています。

@user = User.find_by(current_user.id) @follow_users = @user.followings

しかしこれだとエラーになるようで、
どのようにすれば取り出せますか?

class User follow 関連 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 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) end

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

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

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

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

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

majikadezou

2019/12/24 05:29

回答ありがとうございます。 記事を参考にさせていただいたのですが、解決しません、、、 @user = User.where(id: current_user.id) 記事によるとこのように書けば良さそうなのですが。
winterboum

2019/12/24 06:00 編集

どんなエラーになるか、エラーメッセージを あと、 all_followings  methodのcodeと
majikadezou

2019/12/24 06:30

User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:16:in `current_user' User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 11]] Completed 500 Internal Server Error in 18ms (ActiveRecord: 0.8ms | Allocations: 12675) (current_user のidは1です) undefined method `followings' for # こんなエラーです。 @folow_users = @user.all_followings のところは @folow_users = @user.followings に修正したうえです。 def followings @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end @user = User.find(1) で試してみると普通に機能するのですが。。。
guest

回答1

0

ベストアンサー

@user = User.find_by(current_user.id)
これは余分なアクセスしています
@user = current_user
で良いです。

ここがよくわかりません

@folow_users = @user.followings に修正したうえです。 def followings @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end

@user.followings の followings は User の method ですが、その下の
def folloeings は controllerのmethodのようです。

元々の質問は @folow_users = @user.followings でのエラーの様に書かれています。とすると class User の def followings 若しくは 関連定義が怪しいです。
class User のその辺りのcodeと、もし class Following があるなら、そのcodeも質問に追加してください

投稿2019/12/24 06:45

winterboum

総合スコア23347

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

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

majikadezou

2019/12/24 07:33 編集

@user = current_user で表示はされるようになったのですが、今度はログアウトの際に undefined method `followings' for # が出ます。 質問にコードを追加しました、class User の def followings というのは、 has_many :followings の部分で合っていますでしょうか? それ以外だと書いていないので、書く必要があるのでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問