NoMethodError in Users # index 'undefined method `each' for nil: NilClass'

I am getting NoMethodError on user list # with undefined method `each 'for nil: NilClass' for below line:

<% @users.each do |user| %>

      

I'm not sure where the problem is. Any help is appreciated. Thank you in advance. I know I am missing something extremely lightweight 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

+3


source to share


1 answer


An error NoMethodError

means that you are calling a method that does not exist for the given class. In this case nil:NilClass

, which obviously does not contain a method each

, because it is nil

! So let's consider:

You are calling @users.each

which is causing the error. Since it is said that if undefined method to nil (as described above) tells us that the page index

@users

has a nil

(perhaps not the way it should be!).



So it looks like the problem lies with your definition @users

. I'm not entirely sure, but it looks like UsersController

you have an extra end

after in yours def following

that could cause the controller to terminate prematurely and therefore not be detected @users

. (You can check this by changing it to @users = User.all

which should work).

Let me know how it goes

+12


source







All Articles