ruby on rails - Why does the output is not passing through the ability check in Role Based Authorization (One Role Per User) -
i want 3 user levels admin ,manager,customer in rails application. i've created devise model users , added migration add user role it.so when user signed stores users role(whether admin,a manager or customer). , in application there models , controllers product,delivery,services. , want set access levels each models.
so admin have access models, controllers
manager have access product, delivery
customer have access services
and i've written ability model follows.
class ability include cancan::ability def initialize(user) user ||= user.new # guest user (not logged in) if user.roles == "admin" can :manage , :all elsif user.roles == "manager" can :read, products, delivery elsif user.roles == "customer" can :read, services end end end
my show view product follows.
<% if can? :manage ,@products%> <h1>products</h1> <% @products.each |product| %> <p> <%= product.name%> <p> <%= product.price %><br> <p> <%= product.qty %><br> <%end%> <%end%>
but sign in admin data not displayed. i'm referring following cancan documentation. https://github.com/cancancommunity/cancancan/wiki/role-based-authorization code seems okay "one role per user" data not displayed.please me solve issue.
i'm no real expert @ cancan, may try:
class ability include cancan::ability def initialize(user) user ||= user.new # guest user (not logged in) cannot :manage, :all # can since abilities or'ed if user.roles.include?('admin') can :manage , :all elsif user.roles.include?('manager') can :read, products, delivery elsif user.roles.include?('customer') can :read, services end end end
besides, if it's project start, think cancancan https://github.com/cancancommunity/cancancan
it's updated version of cancan, still maintained community.
Comments
Post a Comment