different user views meme

Rails – Creating different user views – Admin and Beta User Views

Since we’ve been able to create different user types – now we need to set up different user views for these user types to really take advantage of the function.

We’ll create two drop-down menus for our users, one for admins and one for beta users.  Depending on their user type they’ll see one or the other, neither, or both.

Getting started: Dropdowns for different user views

I’m using twitter bootstrap for my navigation menu, which I’ve put in it’s own partial. We’ll first create a dropdown menu that checks with devise to see if the current_user is an admin, and if so, displays an admin menu in the navigation bar:

app/views/layouts/_navigation.html.erb
  <% if current_user.try(:admin?) %>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Admin <span class="caret"></span></a>
    </li>
  <% end %>

Now we can add in a link to let Admin users see our user index:

app/views/layouts?_navigation.html.erb
  <% if current_user.try(:admin?) %>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Admin <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><%= link_to("View Users", users_path) %></li>
      </ul>
    </li>
  <% end %>

Setting up the Beta User dropdown

We can create another Beta drop-down menu by substituting (:admin?) with (:beta?) like this:

app/views/layouts/_navigation.html.erb
  <% if current_user.try(:beta?) %>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Beta <span class="caret"></span></a>
    </li>
  <% end %>

If there are any pages that only beta users should be able to access we would add them to this drop-down. Alternatively, we can also create a beta partial for a page and then only render that partial if the current_user is a beta user. I’ve done the following on my client view:

app/views/clients/show.html.erb
<% if current_user.try(:beta?) %>
  <% if @client.notes.any? %>
    <%= render 'clientnotes' %>
  <% end %>
<% end %>

And then we create the app/views/clients/_clientnotes.html.erb for beta users to see. I’ll have another post coming shortly to see how to create and execute note and contact views and functions for beta viewers for each client in our custom crm.

We can also wield this power in more meaningful ways – like if a certain user hasn’t been singing in as often as they should be so you want to make sure that they understand your appreciation whenever they do sign in. Take for example, my user and friend JJ:

We can set this up in our views folder. Since the root of my application is the app/views/clients/index.html.erb page, we’ll add our conditional code there:

app/views/clients/index.html.erb
<% if current_user.email == "james@hisemailaddress.com" %>
  <h2>Inspirational message</h2>
<% end %>

Now our system will check to see if the current_user has our predefined email address, and if so let him see our appreciation for his participation!hello-jj-conditional-view

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *