我在 Rails.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)
控制器/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
end
意見/用戶/index.html.erb
<% provide(:title, 'All users') %>
<h2>All users</h2>
<%= will_paginate(@users) %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate(@users) %>
意見/用戶/_user.html.erb
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
</li>
uj5u.com熱心網友回復:
這些用戶是否已經在生產資料庫中“確認”?您正在應用過濾器where(confirmed: true),因此如果您可以在“顯示”視圖中看到它們(未應用過濾器),也許這就是原因。
其余發布的代碼似乎很好。或者,您可能需要清理快取并重新啟動服務器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/531282.html
