Home > OS >  Why is Users index page empty (no users loaded) in production? Works locally. Rails 7 Railway.app
Why is Users index page empty (no users loaded) in production? Works locally. Rails 7 Railway.app

Time:11-09

I have a Rails 7 app deployed on Railway.app. Admin users can access a User index page. The User index is working fine locally, but when deployed the page is empty (the page loads, but no users are listed).

Based on the logs, none of the actions in the controller are being called in production, even though they are in local server.

Local server logs:

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)

Production server logs:

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
end

views/users/index.html.erb

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

<%= will_paginate(@users) %>

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

<%= will_paginate(@users) %>

views/users/_user.html.erb

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

CodePudding user response:

Are those users already "confirmed" in production database? You're applying a filtering in where(confirmed: true), so if you can see them in the "show" view (which didn't apply the filter), maybe that's the reason.

The rest of the posted code seems fine. Alternatively, you may need to clean up the cache and restart the server.

  • Related