首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在生产中用户索引页是空的(没有加载用户)?在当地工作。Rails 7+ Railway.app

为什么在生产中用户索引页是空的(没有加载用户)?在当地工作。Rails 7+ Railway.app
EN

Stack Overflow用户
提问于 2022-11-08 11:50:08
回答 1查看 46关注 0票数 0

我在Railway.app上部署了一个Rails 7应用程序。管理员用户可以访问用户索引页面。用户索引在本地运行良好,但部署时页面为空(页面加载,但没有列出用户)。

根据日志,即使控制器位于本地服务器中,也不会在生产中调用控制器中的任何操作。

本地服务器日志:

代码语言:javascript
复制
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)

生产服务器日志:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

views/users/index.html.erb

代码语言:javascript
复制
<% 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

代码语言:javascript
复制
<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.name, user %>
</li>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-09 01:05:49

这些用户是否已经在生产数据库中“确认”了?您要在where(confirmed: true)中应用筛选,所以如果您可以在“显示”视图(没有应用过滤器)中看到它们,也许这就是原因所在。

其余的张贴代码似乎没有问题。或者,您可能需要清理缓存并重新启动服务器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74360252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档