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

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

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

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

Q&A

解決済

1回答

621閲覧

ndefined method `name' for nil:NilClassのエラーが出力される。

kazu_n

総合スコア1

Ruby on Rails

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

0グッド

0クリップ

投稿2020/12/13 12:17

編集2020/12/13 20:57

ndefined method `name' for nil:NilClassのエラーが出て困っています。
![イメージ説明]

下記HTMLで<li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link" >Followings</a></li>を実行した際にエラーが出ます。

HTML

1<div class="row"> 2 <div class="col-sm-12"> 3 <ul class="nav nav-tabs nav-justified mb-3"> 4 <li class="nav-item"><a href="<%= books_path%>" class="nav-link">books</a></li> 5 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link" >Followings</a></li> 6 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link">Followers</a></li> 7 <li class="nav-item"><a href="#" class="nav-link">likes</a></li> 8 <li class="nav-item"><a href="#" class="nav-link">mypages</a></li> 9 </ul> 10 </div> 11</div> 12

下記HTMLでエラーが出ます。

HTML

1<div class="row"> 2 <aside class="col-sm-4"> 3 <div class="card"> 4 <div class="card-header"> 5 <h3 class="card-title"><%= @user.name %></h3> 6 </div> 7 <div class="card-body"> 8 <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> 9 </div> 10 </div> 11 <%= render 'relationships/follow_button', user: @user %> 12 </aside> 13 <div class="col-sm-8"> 14 <ul class="nav nav-tabs nav-justified mb-3"> 15 <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> 16 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li> 17 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li> 18 </ul> 19 <%= render 'users', users: @followings %> 20 </div> 21</div>

コントローラー側は下記です。def following内で@user = User.find(params[:id])を行なっているのですが、上手くview側に受けわたせていないようです。

ruby

1class UsersController < ApplicationController 2 before_action :require_user_logged_in, only: [:index, :show, :followings, :followers] 3 def index 4 @users = User.order(id: :desc).page(params[:page]).per(25) 5 end 6 7 def show 8 @user = User.find(params[:id]) 9 end 10 11 def new 12 @user = User.new 13 end 14 15 def create 16 @user = User.new(user_params) 17 18 if @user.save 19 flash[:success] = 'ユーザを登録しました。' 20 redirect_to @user 21 else 22 flash.now[:danger] = 'ユーザの登録に失敗しました。' 23 render :new 24 end 25 end 26end 27 28def followings 29 @user = User.find(params[:id]) 30 @followings = @user.followings.page(params[:page]) 31 counts(@user) 32end 33 34def followers 35 @user = User.find(params[:id]) 36 @followers = @user.followers.page(params[:page]) 37 counts(@user) 38end 39 40 41private 42 43 def user_params 44 params.require(:user).permit(:name, :email, :password, :password_confirmation) 45 end 46 47```routes.rb 48 49

Rails.application.routes.draw do
root to: 'toppages#index'

get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get 'signup', to: 'users#new'
resources :users, only: [:index, :show, :new, :create]do
member do
get :followings
get :followers
get :likes
end
end

resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :favorites, only: [:create, :destory]
delete 'favorites', to: 'favorites#destroy'
end

```user.rb class User < ApplicationRecord before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 50 } validates :email, presence: true, length: { maximum: 255 }, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }, uniqueness: { case_sensitive: false } has_secure_password has_many :microposts has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverses_of_relationship, source: :user has_many :favorites has_many :darling, through: :favorites, source: :micropost has_many :reverses_of_ravorite, class_name: 'Favorite', foreign_key: 'micropost_id' has_many :favorited, through: :reverses_of_favorite, 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 def feed_microposts Micropost.where(user_id: self.following_ids + [self.id]) end def favorite(micropost) self.favorites.find_or_create_by(micropost_id: micropost.id) end def unfavorite(micropost) favorite = self.favorites.find_by(micropost_id: micropost.id) favorite.destroy if favorite end def favorited?(micropost) self.darling.include?(micropost) end end
class CreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| t.string :name t.string :email t.string :password_digest t.timestamps end end end コード

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

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

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

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

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

m.ts10806

2020/12/13 20:43

ルーティングとモデルの定義を提示してください。 今のところ・・「エラーの通り」になります。
m.ts10806

2020/12/13 20:52

マイグレーションもあったほうがいいですね。
guest

回答1

0

自己解決

class UsersController < ApplicationController
end内に下記が記載されていないことが原因でした。
初歩的なミスで申し訳ありません。
def followings
@user = User.find(params[:id])
@followings = @user.followings.page(params[:page])
counts(@user)
end

def followers
@user = User.find(params[:id])
@followers = @user.followers.page(params[:page])
counts(@user)
end

private

def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

投稿2020/12/16 14:40

kazu_n

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問