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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

Q&A

解決済

1回答

989閲覧

ユーザー名を正しく表示させたい

退会済みユーザー

退会済みユーザー

総合スコア0

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/08/23 15:03

前提・実現したいこと

フォロー機能の実装中です。
ユーザーページからフォロワーとフォローしている他ユーザーの名前を一覧表示させたいのですが、現アクセス中のユーザーページ持ち主の名前がフォロワー数表示されてしまっています。

該当のソースコード

controller

1ユーザー関連 2 3class UsersController < ApplicationController 4 def show 5 @user = User.find(params[:id]) 6 end 7 8 def edit 9 end 10 11 def update 12 if current_user.update(user_params) 13 redirect_to current_user_path 14 else 15 render :edit 16 end 17 end 18 19 def following 20 @title = "Following" 21 @user = User.find(params[:id]) 22 @users = @user.following 23 render 'show_follow' 24 end 25 26 def followers 27 @title = "Followers" 28 @user = User.find(params[:id]) 29 @users = @user.followers 30 render 'show_follow' 31 end 32 33 private 34 def user_params 35 params.require(:user).permit(:name, :email, :password, :password_confirmation, :birthday, :introduction) 36 end 37end

controller

1フォロー・フォロワー関連 2 3class RelationshipsController < ApplicationController 4 def create 5 @user = User.find(params[:followed_id]) 6 current_user.follow(@user) 7 respond_to do |format| 8 format.html { redirect_to @user } 9 format.js 10 end 11 end 12 13 def destroy 14 @user = Relationship.find(params[:id]).followed 15 current_user.unfollow(@user) 16 respond_to do |format| 17 format.html { redirect_to @user } 18 format.js 19 end 20 end 21end

routes

1Rails.application.routes.draw do 2 devise_for :users 3 root to: "rooms#index" 4 5 resources :users, only: [:show, :edit, :update] 6 resources :rooms, only: [:index, :new, :create, :show] do 7 resources :posts, only: [:index, :create] 8 end 9 resources :relationships, only: [:create, :destroy] 10 11 resources :users do 12 member do 13 get :following, :followers 14 end 15 end 16end

models

1ユーザー関連 2 3class User < ApplicationRecord 4 # Include default devise modules. Others available are: 5 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :validatable 8 9 has_many :rooms 10 has_many :posts 11 has_many :active_relationships, class_name: "Relationship", 12 foreign_key: "follower_id", 13 dependent: :destroy 14 has_many :passive_relationships, class_name: "Relationship", 15 foreign_key: "followed_id", 16 dependent: :destroy 17 18 has_many :following, through: :active_relationships, source: :followed 19 has_many :followers, through: :passive_relationships, source: :follower 20 21 # ユーザーをフォローする 22 def follow(other_user) 23 following << other_user 24 end 25 26 # ユーザーをフォロー解除する 27 def unfollow(other_user) 28 active_relationships.find_by(followed_id: other_user.id).destroy 29 end 30 31 # 現在のユーザーがフォローしてたらtrueを返す 32 def following?(other_user) 33 following.include?(other_user) 34 end 35 36 37 with_options presence: true do 38 validates :password, length: { minimum: 6 }, format: {with: /\A(?=.*?[a-z])(?=.*?[\d])[a-z\d]+\z/i} 39 validates :name 40 validates :birthday 41 end 42 43 validates :introduction, length: {maximum: 150} 44end

models

1フォロー・フォロワー関連 2 3class Relationship < ApplicationRecord 4 belongs_to :follower, class_name: "User" 5 belongs_to :followed, class_name: "User" 6 7 validates :follower_id, presence: true, uniqueness: true 8 validates :followed_id, presence: true, uniqueness: true 9end

views

1ユーザー詳細ページ(マイページ) 2 3<%= render "shared/header" %> 4 5<div class='account-page' id='account-page'> 6 <div class='account-page__inner clearfix'> 7 <div class='account-page__inner--left account-page__header'> 8 <h2><%= @user.name %></h2> 9 <%= link_to "編集する", edit_user_path, class: 'btn'%> 10 <%= link_to "チャットページに戻る", rooms_path, class: 'btn'%> 11 <div id="follow_form"> 12 <% if signed_in? current_user == @user %> 13 <% if current_user.following?(@user) %> 14 <%= render 'unfollow' %> 15 <% else %> 16 <%= render 'follow' %> 17 <% end %> 18 <% end %> 19 </div> 20 </div> 21 <div class='account-page__inner--right user-form'> 22 <%= form_with model: @user, local: true do |f|%> 23 24 <div class='field'> 25 <div class="stats"> 26 <%= render 'users/stats' %> 27 </div> 28 </div> 29 30 <div class='field'> 31 <div class='field-label'> 32 <%= f.label :Birthday %><%= @user.birthday %> 33 </div> 34 </div> 35 36 <div class="field"> 37 <div class="field-label"> 38 <%= f.label :Introduction %> <%= @user.introduction %> 39 </div> 40 </div> 41 <% end %> 42 </div> 43 </div> 44</div>

views

1フォロー・フォロワー名一覧ページ 2*問題の箇所 3 4<%= render "shared/header" %> 5 6<div class="row"> 7 <section class="stats"> 8 <%= render 'users/stats' %> 9 <% if @users.any? %> 10 <div class="user_avatars"> 11 <% @users.each do |users| %> 12 <%= link_to @user.name, user_path(users.id) %> 13 <% end %> 14 </div> 15 <% end %> 16 </section> 17</div>

db

1フォロー・フォロワー関連 2 3class CreateRelationships < ActiveRecord::Migration[6.0] 4 def change 5 create_table :relationships do |t| 6 t.integer :follower_id 7 t.integer :followed_id 8 t.timestamps 9 end 10 add_index :relationships, :follower_id 11 add_index :relationships, :followed_id 12 add_index :relationships, [:follower_id, :followed_id], unique: true 13 end 14end

試したこと

仮説としてcurrent_userをどこかに誤記入しているかなと見直しましたが、見つけられず、
そもそも問題点はログインユーザー名が表示されているのではなく、アクセス先のユーザーの名前が一覧に表示されているので関係はないものでした。
ユーザーidが怪しいと考えています。

アプリ完成のために御助力、御助言よろしくお願い致します。

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

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

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

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

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

winterboum

2021/08/23 23:11

「現アクセス中のユーザーページ持ち主の名前がフォロワー数表示されて」 ここよくわかりません
退会済みユーザー

退会済みユーザー

2021/08/24 04:47

winterboumさんいつもありがとうございます! 例えば、フォロワー数5人のhogeさんのフォロワー詳細ページにとぶと、各フォロワーの名前ではなくhoge が5つ表示されている状態です。
guest

回答1

0

ベストアンサー

view は ひとつ上のdirからのfile名をつけていただかないとどのcontrollerのどのactionからよばれたか、 がわからないので、esperモードになってしまいます。
フォロー・フォロワー名一覧ページ がUsersController#followers からよばれているとして考えます。

<% @users.each do |users| %> <%= link_to @user.name, user_path(users.id) %>

ではなく

<% @users.each do |user| %> <%= link_to user.name, user_path(user.id) %>

に。
なお、@users ではなく @follwers にしたほうがよいです。
今回みたいな勘違い起きにくいし

投稿2021/08/24 05:23

winterboum

総合スコア23329

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

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

退会済みユーザー

退会済みユーザー

2021/08/24 07:32

無事解決できました。 ありがとうございます! 質問の仕方気をつけます。 たくさんのご協力ありがとうございます!
winterboum

2021/08/25 01:40

質問の仕方で実力ばれちゃいますし、、、、
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問