My objective is to have a User Admin menu option in the navbar that is enabled if the current user is an admin user. The admin user can select this and will be routed to the users_path which in rails routes is:
users GET /users(.:format) users#index
Within app/views/users/index.html.erb is a table which displays username and email currently. The end objective is to also have an admin checkbox so an admin user can check this against a user to change the role to 'admin' once submitted for that user. Basically a User Administration page to administer other users.
However, at the moment the table just has username and email fields in the table. The problem is that no data can be seen in this table. It is blank. BUT when I use debug I can see all the data in the debug panel when the page loads. user index page with table empty but debug panel populated with user data
Here is how it is currently setup:
1) routes.rb:
devise_for :users
resources :users, only: [:index]
2) users_controller.rb
class UsersController < ApplicationController
load_and_authorize_resource
def index
@users = User.all
end
end
3) index.html.erb
<table id="user_list" class="display table table-striped table-sm table-hover" cellspacing="0" width="100%" >
<thead>
<tr>
<th>Username</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.username%></td>
<td><%= user.email %></td>
</tr>
<% end %>
</tbody>
</table>
4) user model:
class User < ApplicationRecord
rolify
devise :database_authenticatable, :rememberable, :trackable, :registerable
attr_accessor :username, :email, :password, :password_confirmation, :remember_me, :firstname, :lastname
after_create :assign_default_role
def assign_default_role
self.add_role(:user) if self.roles.blank?
end
end
5) application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!
protected
def configure_permitted_parameters
added_attrs = %i[username email password password_confirmation remember_me]
devise_parameter_sanitizer.permit :sign_in, keys: added_attrs
end
end
6) cancan ability class
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :admin
can :manage, :all
else
can :read, :all
end
end
end
Any ideas what is preventing the user data to display in the index.html.erb?
pry
orbyebug
to inspect the@users
variable. What does@users.size
give you for example? – max