Question

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
Was it helpful?

Solution

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

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top