I'm getting a NoMethodError in Users#Index with an 'undefined method `each' for nil:NilClass' for the line below:
<% @users.each do |user| %>
I'm not sure where the problem resides. Any help is greatly appreciated. Thanks in advance. I know I am missing something extremely easy here.
User/Index
<div class="page-header">
<center><strong><h1> All Users </h1></strong></center>
</div>
<div class="row">
<% @users.each do |user| %>
<div class="horizontal-align col-md-2">
<div class="user">
<center><%= link_to image_tag(user.avatar.url(:thumb)), user %></center>
<center><br><%= link_to user.name, user %></br></center>
<% if current_user.admin %>
<center><%= link_to "Delete", user, method: :delete, data: { confirm: "Are you sure?" } %></center>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
<div class="center">
<%= will_paginate @users, renderer: BootstrapPagination::Rails %>
</div>
</div>
User/Show
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= image_tag @user.avatar.url(:thumb) %>
<%= @user.name %>
</h1>
<section>
<%= render 'users/stats' %>
</section>
<% if current_user.following?(@user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
User/Controller
class UsersController < ApplicationController
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :admin_user, only: :destroy
def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
def index
@users = User.paginate(page: params[:page], :per_page => 20)
end
def show
@user = User.find(params[:id])
if @user
@posts = @user.posts.order("updated_at DESC")
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "Your account has been deleted."
redirect_to root_path
end
def correct_user
@user = User.find(params[:id])
redirect_to root_path
end
def admin_user
redirect_to root_path unless current_user.admin?
end
end
The error NoMethodError
means that you are calling a method that doesn't exist for the given class. In this case it is nil:NilClass
which, obvious, doesn't contain the method each
because it is nil
! So let's examine:
You are calling @users.each
which is calling the error. Since it says that the method if undefined for nil (as described above) it tells us that on the index
page, @users
is nil
(probably not what it is intended to be!).
So it looks like the problem rests in your definition of @users
. I'm not entirely sure, but it looks as though, in your UsersController
that you have an extra end
after the def following
which would cause the controller to end prematurely, and therefore not define @users
. (You can test that by changing it to @users = User.all
which should work).
Let me know how things turn out