ruby-on-rails 为什么在生产中用户索引页为空(没有加载用户)?在本地工作,Rails 7 + Railway.app

pkln4tw6  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(142)

我在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)

控制器/用户控制器.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

视图/用户/索引.html.erb

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

<%= will_paginate(@users) %>

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

<%= will_paginate(@users) %>

视图/用户/_用户.html.erb

<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
</li>
68bkxrlz

68bkxrlz1#

这些用户已经在生产数据库中“确认”了吗?您正在where(confirmed: true)中应用过滤器,所以如果您可以在“show”视图中看到他们(没有应用过滤器),这可能就是原因。
发布的代码的其余部分看起来很好。或者,您可能需要清理该高速缓存并重新启动服务器。

相关问题