ruby – @users变量为空.忘记传递will_paginate的集合对象?

前端之家收集整理的这篇文章主要介绍了ruby – @users变量为空.忘记传递will_paginate的集合对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题localhost:3000 / users /将不会显示

我谦卑地进入,因为我第一次尝试通过rails教程.我在第10章,我在拍摄这个问题5个小时都遇到了麻烦.当我尝试访问localhost:3000 / users /我得到一个错误(我相信这与factory_girl有关),它解释了@users变量为空并且我忘了为will_paginate传递一个集合对象.
我目前在第10章第10.23节,每次我跑:

$bundle exec rake db:reset

$bundle exec rake db:populate

$bundle exec rake db:test:prepare

我得到一个错误解释

rake aborted!
Factory already registered: micropost

这是我第二次尝试这一章,因为我第一次遇到问题并从第9章开始.请在提供指示时帮助并清楚详细.我很乐意发布任何有用的文件.

这是我的index.html.erb – 我将它们保存为HTML,它们是否应保存为ruby文件

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

<%= will_paginate %>

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

<%= will_paginate %>

这是我的用户控制器

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

解决方法

在你的用户控制器中,确保你有@users,如果你正在使用will_paginate,请确保你调用.paginate(页面:params [:page],per_page:20]并在你的视图中,有<%= will_paginate @users %取代.
原文链接:https://www.f2er.com/ruby/264605.html

猜你在找的Ruby相关文章