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 コード
回答1件
あなたの回答
tips
プレビュー