ruby - uninitialized constant Ability Rails -


i have gone through different solutions given problem none of them working please don't try close question duplicate.

i have role column in users table. user can admin or user , need put permissions on base of user role using cancan. want give permissions admin. logged in admin when access /users error uninitialized constant ability , when remove load_and_authorize_resource cancan permission doesn't work.my ability class looks like

class ability   include cancan::ability    def initialize(user)     #abort("message goes here")     user ||= user.new # guest user     #abort('some user')     if user.role == 'admin'       can :manage, :all     elsif user.role == 'user'       can :manage, micropost |micropost|         micropost.try(:owner) == user       end       can :update, user |users|         users.try(:owner) == user       end     else      can :read, :all     end   end end 

in userscontroller having

class userscontroller < applicationcontroller   load_and_authorize_resource   #devise code   before_filter :authenticate_user!, only: [:index, :edit, :update, :destroy, :following, :followers]   blah blah  end 

and routes file looks like

firstapp::application.routes.draw    devise_for :users    resources :users   member     :following, :followers   end end #resources :sessions, only: [:new, :create, :destroy] resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy]  root to: "static_pages#home"  match '/help', to: 'static_pages#help' match '/about', to: 'static_pages#about' match '/contact', to: 'static_pages#contact' end 

you seeing uninitialized constant ability because load_and_authorize_resource method in userscontroller expects find ability class.

the solution move file containing ability definitions app/models/ability.rb.

#app/models/ability.rb class ability   include cancan::ability    def initialize(user)     #abort("message goes here")     user ||= user.new # guest user     #abort('some user')     if user.role == 'admin'       can :manage, :all     elsif user.role == 'user'       can :manage, micropost |micropost|         micropost.try(:owner) == user       end       can :update, user |users|         users.try(:owner) == user       end     else      can :read, :all     end   end end 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -