I apologize if this is a dumb question, but it looks like I am still not sure how Rails is supposed to work when it comes to making extra pages.
I have a devise user model (and a user controller) which has an integer field "account_type" for each user - 0 for Admin, 1 for Manager and 2 for Caller.
I know how to use the index page to show all types of users. However, how do I make a page that shows results based on what I click on? For example, I have 3 buttons in the menu:
When I click on Admins, I should see only the admins. When I click on Managers, I should see only managers etc.
That's all I have for the controller and now sure how to change it, but also not sure how to even link the new pages in the views.
class UsersController < ApplicationController
def index
@users = Users.all
end
end
The simple and RESTful way would be to just create different routes for each type:
resources :admins, only: [:index]
resources :managers, only: [:index]
resources :callers, only: [:index]
This creates the routes /admins
, /managers
, /callers
that each go to a separate controller:
class AdminsController
# GET /admins
def index
@users = User.admin
render 'users/index'
end
end
class ManagersController
# GET /managers
def index
@users = User.manager
render 'users/index'
end
end
class CallersController
# GET /callers
def index
@users = User.caller
render 'users/index'
end
end
To create a button that goes to each type of user the simplest and most accessible way is to just use good old links and style them to look like a button.
<%= link_to 'Admins', admins_path %>
<%= link_to 'Managers', managers_path %>
<%= link_to 'Callers', callers_path %>
You also want to setup ActiveRecord::Enum to handle your integer column:
class User < ApplicationRecord
# ...
enum account_type: {
admins: 0,
managers: 1,
callers: 2
}
end
The next step would be to refactor and cut duplication if needed.