我刚刚完成了michael hartl第9课的轨道教程。 在localhost上,一切看起来都很好,但是当我在heroku上部署应用程序时,一些用户在用户列表中的错误位置(例如,示例用户必须在第一页,但它在第二页)。为什么会这样?
我的申请:https://mordor-depth.herokuapp.com/users
您可以与示例用户一起唱歌: example@railstutorial.org foobar的
对不起,对于一个愚蠢的问题:)
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.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 the Sample App!"
redirect_to @user
else
render 'new'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success]='User deleted!'
redirect_to users_url
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def edit
end
def update
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
#Before filters
def signed_in_user
store_location
redirect_to signin_url, notice:'Please sign in.' unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
index.html.erb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
_user.html.erb
<li>
<%= gravatar_for user, size:52 %>
<%=link_to user.name, user %>
<% if current_user.admin? && !current_user?(user)%>
| <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
<% end %>
</li>
答案 0 :(得分:2)
您可以在索引操作上看到您没有进行任何排序:
def index
@users=User.paginate(page: params[:page])
end
您可以轻松按名称命令
def index
@users = User.order(:name).paginate(page: params[:page])
end