ruby on rails 4 - How do I restrict a specific scope just to users with a specific role? -



ruby on rails 4 - How do I restrict a specific scope just to users with a specific role? -

so have post model. posts can either published or unpublished. using rolify, cancancan , devise.

what want happen :admin users, should able view post#show action of posts, :member or guest users (i.e. non-logged in) should ever able see post.published posts.

my ability.rb looks this:

if user.has_role? :admin can :manage, :all #member elsif user.has_role? :member can :read, :all can :create, post can :status, post can :update, post |post| post.try(:user) == user end #guest else can :read, :all can :create, post can :status, post end

i tried doing this, both :member , guest, gave me endless redirect loop on post#index page - root_path:

can :read, post.published

where post.published returns array of posts publication_status = "published".

this how declared on post.rb:

enum publication_status: [ :unpublished, :published ]

how accomplish this?

i figured out.

in post#show, did this:

def show if current_user , current_user.has_role? :admin or :editor @post = post.find(params[:id]) else @post = post.published.find(params[:id]) end end

that works charm.

if has more elegant way, within ability.rb love know.

i tried many permutations using blocks , jazz in ability.rb nil worked.

i had remove default added set_post method in controller, because cancancan loads_and_authorize resource.

ruby-on-rails-4 devise cancan rolify cancancan

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -