So what I would like to do is to do redirects based on the role of the current_user
.
This is what I have:
path = case current_user.roles.where(:name => "vendor")
when :vendor
dashboard_path
when :guest
home_path
else
home_path
end
redirect_to path
I am using cancan
and the only way to figure out the role of a user, that I know of, is to either do current_user.has_role? :admin
or current_user.roles.where(:name => role_name)
.
Given those constraints (or tell me another way to figure out the role of a user) how do I get this case statement to work?
Edit 1
Assume that I am checking for multiple roles, not just the 2 I have here - could be 4 or 5.
Edit 2
To be clear, this is my current setup.
I am using Devise, CanCan & Rolify. Rolify allows a user to have multiple roles, but my application won't have that use case. A user will just have one role. They can either be a vendor, buyer, guest, superadmin
.
If they are a vendor
, they can only see the dashboard_path
that belongs to them. They can't see any other vendor storefront
that belongs to anyone else. They also should not be able to see products from other vendors. So, once they login, their root_path
should be dashboard_path
not home_path
which is what every other role's root_path
will be.
If they are a guest
, they can see everything except the prices - I already have this logic working. I achieved this like this:
if user.has_role? :guest
can :read, [Product, Vendor, Banner]
cannot :view_prices, Product
end
Then in my view, I just did something like this:
<% if can? :view_prices, Product %>
<div class="price pull-right">
<%= number_to_currency(@product.price) %> ea
</div>
<% else %>
<span class="no-price pull-right"><%= link_to "Log In To See Price", login_path %></span>
<% end %>
So, basically...my real goal is to try and change the root_path
depending on the role the user has. I am basically trying to implement the answer on this question.