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

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

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

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

Q&A

0回答

975閲覧

follower一覧に、ユーザー情報を表示させたい。

nika_nika

総合スコア38

Ruby on Rails

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

0グッド

0クリップ

投稿2020/12/15 10:26

解決したいこと

フォローされているユーザーからの情報を表示させたい。

問題となっている箇所

イメージ説明

@profileから情報を抜き取りたいのですが、エラーが起ってしまします。ログでは、インスタンスにフォローしているユーザーの情報が入ってると思っているのですが、表示させることができなかったです。。

userとcompanyモデルを作成してuserからフォローをできるようにしています。

参考

config/routes.rb

Rails.application.routes.draw do devise_for :companies, controllers: { sessions: 'companies/sessions', passwords: 'companies/passwords', registrations: 'companies/registrations' } devise_for :users, controllers: { sessions: 'users/sessions', passwords: 'users/passwords', registrations: 'users/registrations' } # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root to: 'top#index' resources :articles do resource :like, only: [:show, :create, :destroy] end resource :following_list, only: [:show] resource :follower_list, only: [:show] resources :relationships, only: [:show] do resources :follows, only: [:create] resources :unfollows, only: [:create] end resource :profile, only: [:show, :edit, :update] resource :company_profile, only: [:show, :edit, :update] resources :favorites, only: [:index] end

userモデル

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 :likes, dependent: :destroy has_many :favorite_articles, through: :likes, source: :article has_one :profile, dependent: :destroy has_many :relationships, dependent: :destroy has_many :followings, through: :relationships, source: :company delegate :birthday, :age, :gender, to: :profile, allow_nil: true def has_liked?(article) likes.exists?(article_id: article.id) end def display_name profile&.nickname || self.email.split('@').first end def prepare_profile profile || build_profile end def follow!(company) company_id = get_compay_id(company) relationships.create!(company_id: company_id) end def unfollow!(company) company_id = get_compay_id(company) relation = relationships.find_by!(company_id: company_id) relation.destroy! end def has_followed?(company) relationships.exists?(company_id: company.id) end def avatar_image if profile&.avatar&.attached? profile.avatar else 'default-avatar.png' end end private def get_compay_id(company) if company.is_a?(Company) company.id else company end end end

companyモデル

class Company < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :articles, dependent: :destroy has_many :relationships, dependent: :destroy has_many :followers, through: :relationships, source: :user has_one :company_profile, dependent: :destroy def has_written?(article) articles.exists?(id: article.id) end def display_name company_profile&.company_name || self.email.split('@').first end def prepare_company_profile company_profile || build_company_profile end def avatar_image if company_profile&.avatar&.attached? company_profile.avatar else 'default-avatar.png' end end end

profileモデル

class Profile < ApplicationRecord enum gender: { male: 0, female: 1, other: 2 } belongs_to :user has_one_attached :avatar def age return '不明' unless birthday.present? years = Time.zone.now.year - birthday.year days = Time.zone.now.yday - birthday.yday if days < 0 "#{years - 1}歳" else "#{years}歳" end end end

follower_lists_controller.rb

class FollowerListsController < ApplicationController before_action :authenticate_company! def show user_ids = current_company.followers.pluck(:id) @profile = Profile.where(user_id: user_ids) end end

profiles_controller.rb

class ProfilesController < ApplicationController before_action :authenticate_user! def show @profile = current_user.profile end def edit @profile = current_user.prepare_profile end def update @profile = current_user.prepare_profile @profile.assign_attributes(profile_params) if @profile.save redirect_to profile_path, notice: 'プロフィール更新!' else flash.now[:error] = '更新できませんでした' render :edit end end private def profile_params params.require(:profile).permit( :nickname, :last_name, :first_name, :kana_last_name, :kana_first_name, :introduction, :gender, :birthday, :subscribed, :avatar, :address, :phone_number ) end end

view/follower_lists/show.html.erb

<%= image_tag @profile.user.avatar_image %>

情報量多くてすいません。いらない情報、足りない情報があればご教示お願いいたします。

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問