Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@users variable empty. forget to pass the collection object for will_paginate?

Problem localhost:3000/users/ won't display

I enter humbly as I am trying to make it through the rails tutorial for the first time. I am in chapter 10 and I have been trouble shooting this for 5 hours. When I attempt to visit localhost:3000/users/ I get an error (I believe this has something to do with factory_girl) that explain that the @users variable is empty and that I forgot to pass a collection object for will_paginate.

I'm currently at chapter 10, section 10.23 and each time I run:

$ bundle exec rake db:reset

$ bundle exec rake db:populate

$ bundle exec rake db:test:prepare

I get an error explaining that

rake aborted! Factory already registered: micropost

This is my second time trying this chapter as I encountered problems the first time and started from chapter 9. Please help and be clear and detailed when providing directions. I am happy to post whatever files that will be helpful.

Here is my index.html.erb - I save these as HTML, should they be saved as ruby files instead?

<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @users %>
</ul>

<%= will_paginate %>

Here is my users controller

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_filter :correct_user,   only: [:edit, :update]
  before_filter :admin_user,     only: :destroy

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end
end

   def new
    @user = User.new
   end

 def index
    @title = "All users"
    @users = User.paginate(:page => params[:page])
  end

def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Do more of the things you love!"
      redirect_to @user
    else
      render 'new'
    end
  end

def edit
  end

  def update
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_url
  end

private

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      redirect_to(root_path) unless current_user.admin?
    end
like image 305
Marquett Davon Burton Avatar asked Dec 19 '12 15:12

Marquett Davon Burton


1 Answers

In your Users controller, make sure you have @users and if you are using will_paginate, make sure you call .paginate(page: params[:page], per_page: 20] and in your view, have <%= will_paginate @users %>.

like image 200
Tommy DANGerous Avatar answered Nov 11 '22 16:11

Tommy DANGerous