我在Railway.app上部署了一个Rails 7应用程序。管理员用户可以访问用户索引页面。用户索引在本地运行良好,但部署时页面为空(页面加载,但没有列出用户)。
根据日志,即使控制器位于本地服务器中,也不会在生产中调用控制器中的任何操作。
本地服务器日志:
Started GET "/en/users" for ::1 at 2022-11-08 13:38:38 +0200
Processing by UsersController#index as HTML
Parameters: {"locale"=>"en"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:18:in `admin_user'
Rendering layout layouts/application.html.erb
Rendering users/index.html.erb within layouts/application
User Count (0.4ms) SELECT COUNT(*) FROM "users" WHERE "users"."confirmed" = $1 [["confirmed", true]]
↳ app/views/users/index.html.erb:4
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."confirmed" = $1 LIMIT $2 OFFSET $3 [["confirmed", true], ["LIMIT", 30], ["OFFSET", 0]]
↳ app/views/users/index.html.erb:7
Rendered collection of users/_user.html.erb [30 times] (Duration: 4.6ms | Allocations: 3858)
Rendered users/index.html.erb within layouts/application (Duration: 11.0ms | Allocations: 6653)
Rendered layouts/_bootstrap_cdn.html.erb (Duration: 0.0ms | Allocations: 16)
Rendered layouts/_rails_default.html.erb (Duration: 7.3ms | Allocations: 5483)
Rendered layouts/_shim.html.erb (Duration: 0.0ms | Allocations: 15)
Rendered shared/_search_bar.erb (Duration: 0.0ms | Allocations: 24)
Rendered user/_session_manager.html.erb (Duration: 0.5ms | Allocations: 366)
Rendered shared/_search_modal.erb (Duration: 0.6ms | Allocations: 592)
Rendered layouts/_header.html.erb (Duration: 1.8ms | Allocations: 1355)
Rendered layouts/_messages.html.erb (Duration: 0.0ms | Allocations: 27)
Rendered layouts/_footer.html.erb (Duration: 0.5ms | Allocations: 158)
Rendered layout layouts/application.html.erb (Duration: 23.8ms | Allocations: 15919)
Completed 200 OK in 31ms (Views: 23.6ms | ActiveRecord: 1.2ms | Allocations: 17431)生产服务器日志:
I, [2022-11-08T11:39:47.740716 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Started GET "/en/users" for 79.183.112.167 at 2022-11-08 11:39:47 +0000
I, [2022-11-08T11:39:47.741782 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Processing by UsersController#index as HTML
I, [2022-11-08T11:39:47.741846 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Parameters: {"locale"=>"en"}
I, [2022-11-08T11:39:47.753841 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Rendered users/index.html.erb within layouts/application (Duration: 3.9ms | Allocations: 414)
I, [2022-11-08T11:39:47.758103 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Rendered layout layouts/application.html.erb (Duration: 8.2ms | Allocations: 2537)
I, [2022-11-08T11:39:47.758466 #19] INFO -- : [8cc8cc81-a68c-4ae6-80a8-a68b31ae0a34] Completed 200 OK in 17ms (Views: 6.5ms | ActiveRecord: 4.2ms | Allocations: 3549)contollers/users_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!, only: :show
before_action :admin_user, :only => [:index, :edit, :update, :destroy]
def show
@user = User.find(params[:id])
end
def index
@users = User.where(confirmed: true).paginate(page:params[:page])
end
private
def admin_user
redirect_to(root_url, status: :see_other) unless
current_user && current_user.admin?
end
endviews/users/index.html.erb
<% provide(:title, 'All users') %>
<h2>All users</h2>
<%= will_paginate(@users) %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate(@users) %>views/users/_user.html.erb
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
</li>发布于 2022-11-09 01:05:49
这些用户是否已经在生产数据库中“确认”了?您要在where(confirmed: true)中应用筛选,所以如果您可以在“显示”视图(没有应用过滤器)中看到它们,也许这就是原因所在。
其余的张贴代码似乎没有问题。或者,您可能需要清理缓存并重新启动服务器。
https://stackoverflow.com/questions/74360252
复制相似问题