Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Last active December 24, 2015 09:39
Show Gist options
  • Save TechFounder/6778767 to your computer and use it in GitHub Desktop.
Save TechFounder/6778767 to your computer and use it in GitHub Desktop.
This is code so that the nav bar will show the correct corresponding user page where the devise gem is used to create 3 separate user classes: User, Admin, Company User. I think there are instances where developers who choose not to go down the STI route. There are 2 parts to the code. First a header partial where the conditions are set for the …
<ul class="left">
<% if user_signed_in? %>
<li><%= link_to "Home Page", root_path %></li>
<% elsif admin_signed_in? %>
<li><%= link_to "Home Page", root_path %></li>
<li><%= link_to "Admin Home Page", admin_path %></li>
<% elsif company_user_signed_in? %>
<li><%= link_to "Home Page", root_path %></li>
<li><%= link_to "Company Home Page", company_path %></li>
<% else %>
<li><%= link_to "Home Page", root_path %></li>
<li><%= link_to "Admin Home Page", admin_path %></li>
<li><%= link_to "Company Home Page", company_path %></li>
<% end %>
</ul>
<ul class="right">
<% if user_signed_in? %>
<li><%= link_to "Edit Profile", edit_user_registration_path %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% elsif admin_signed_in? %>
<li><%= link_to "Edit Profile", edit_admin_registration_path %></li>
<li><%= link_to "Logout", destroy_admin_session_path, method: :delete %></li>
<% elsif company_user_signed_in? %>
<li><%= link_to "Edit Profile", edit_company_user_registration_path %></li>
<li><%= link_to "Logout", destroy_company_user_session_path, method: :delete %></li>
<% elsif params[:action] == 'company_user' %>
<li><%= link_to "Company Login", new_company_user_session_path %></li>
<% elsif params[:action] == 'admin' %>
<li><%= link_to "Admin Login", new_admin_session_path %></li>
<% else %>
<li><%= link_to "User Login", new_user_session_path %></li>
<% end %>
</ul>
# Stick this in your applciation controller file somewhere so that after you sign in, you will be taken to the correct user page
def after_sign_in_path_for(resource)
case resource
when User then '/'
when Admin then '/admin'
when CompanyUser then '/company'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment