Structuring the Page Flow for Admins vs Employees
Learn how to configure the homepage page flow so that managers see a completely different dashboard than employees.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this video, let's play a little bit around the design for our home page. As of now, I'm thinking that if the user is an admin, he or she should be able to see all the latest items including posts that have to be approved. For employees, it should be an option to verify their time.

Open homepage.html.erb and remove all the code inside it. Also, I'm going to create a couple of partials inside the static folder. One is going to be _employee.html.erb and the other is going to be _admin.html.erb. We'll place a basic content inside each partial, something like "Hi Admin" and "Hi employee" respectively.

In homepage.html.erb, I'm going to check the user type and call the corresponding partial.

<!-- app/views/static/homepage.html.erb -->

<% if current_user.try(:type) == 'AdminUser' %>
  <%= render 'admin' %>
<% else %>
  <%= render 'employee' %>
<% end %>

Refresh the browser, and you should see "Hi Admin" if you're logged in as an admin and "Hi Employee" if you're logged in as a regular user.

We obviously need to refactor this code because it's too fragile. Open application_helper.rb, and create a method called admin_types, and this will return an array of values, though for now, we have only AdminUser.

# app/helpers/application_helper.rb

def admin_types
  ['AdminUser']
end

Now with this change in place we can refactor the code like this:

<!-- app/views/static/homepage.html.erb -->

<% if admin_types.include?(current_user.try(:type)) %>
  <%= render 'admin' %>
<% else %>
  <%= render 'employee' %>
<% end %>

That's working nicely. This is better because we can add more user types to the admin_types method in the future, instead of making a change everywhere in the application.

Resources