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

3

6

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
Prurigo answered 19/12, 2012 at 15:7 Comment(0)
G
8

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 %>.

Granadilla answered 19/12, 2012 at 15:39 Comment(4)
I have changed my users controller line from atusers = User.paginate(:page => params[:page]) to atusers = User.paginate(page: params[:page], [per_page: 20] <code> is that what you were suggesting? I am getting a new error now. It is syntax error, unexpected '\n', expecting tASSOCPrurigo
@John what do I type to indicate to this text box that I am entering code?Prurigo
See this page which explains how this site uses markupApplewhite
Tommy actually helped me via gchat to fix the problem which was more complete than what was on here.Prurigo
D
0

/users should point to UsersController#index. Make sure you are assigning the collection @users.

For instance it could look like this at the most basic level:

def index
  @users = User.all # not paginated
  @users = User.paginate(page: params[:page]) # paginated
end

As far as the test database error, I'm guessing that's because you define a :micropost factory more than once.

Desiraedesire answered 19/12, 2012 at 15:24 Comment(1)
AJcodez, thanks for help. I tried this but it did not work. I knew this was where the issue was so i have been playing with variations to no avail. Where would I find the definition of :micropost factory?Prurigo
D
0

Instead of @user = User.find(params[:id]) this you should use @users = User.paginate(page: params[:page])

Deploy answered 20/10, 2020 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.