我正在关注 Hartl 的 RubyonRails 教程(第 9 章)。
我的本地计算机上的所有用户列表显示为根据他们的用户 ID。
由于“AKS”(用户名的 id = 1 并且也是此页面的管理员)显示在列表顶部,但在 Herokuapp 上传后相同的内容,列表显示不同。
第一个用户出现在最后一页,但第二个及以后的用户仍显示在第一页。谁能建议我如何在 Herokuapp.com 上正确显示列表我已附上在 localhost 和 heroku 上显示的列表图像。
第一张图片是在本地机器上显示的列表,第二张是在heroku上显示的列表
users_controller.rb 的内容
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
#@users = User.all
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to sample app"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
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 user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# below code for signed_in_user can also be written as
#def signed_in_user
# redirect_to signin_url, notice: "Please sign in." unless signed_in?
#end
# Before filters
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_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
index.html.erb 的内容
<% provide(:title, 'All users') %>
<h2>All users</h2>
<%= will_paginate %>
<ul class="users">
<% @users.each do |user| %>
<%= render @users %> <!--"render user" or "render @user" in Rails will search for _user.html.erb file-->
<% end %>
</ul>
<%= will_paginate %>
告诉我是否需要发布更多代码。