Comments (2)
I'm guessing the error you are getting is that Admin::Article doesn't exist. Unfortunately there isn't anything that gets passed to Ability which can be used to determine the namespace you're in.
I recommend setting up an in-between controller which all admin controllers can inherit from. This is often a good thing to do with any namespace because it can help refactor out common functionality that exists in the controllers in that namespace.
# in controllers/admin/base_controller.rb module Admin class BaseController < ApplicationController # common behavior goes here ... end end
Then use this as the superclass for each of those controllers.
# in controllers/admin/articles_controller.rb module Admin class ArticlesController < BaseController # ... end end
Now several possibilities open up, and the one to go with depends on how complex the authorization needs to be in this admin namespace. If all you need to do is verify User.admin? then you may want to skip CanCan altogether and use a before filter to check this.
# in controllers/admin/base_controller.rb before_filter :verify_admin private def verify_admin redirect_to root_url unless current_user.admin? end
There's really no need to use CanCan here because all the behavior is the same. However if you have various levels of admins which needs to access different things then that's a reason to use CanCan. In that case I recommend creating a separate Ability class.
# in models/admin_ability.rb class AdminAbility include CanCan::Ability def initialize(user) # define admin abilities here .... end end
Then use this AdminAbility for admin controllers.
# in admin/base_controller.rb def current_ability AdminAbility.new(current_user) end
Does that make sense? I should probably turn this into a wiki page.
from cancan.
Does this:
http://wiki.github.com/ryanb/cancan/authorization-for-namespaced-controllers
not provide a reasonable solution?
from cancan.
Related Issues (20)
- User Interface HOT 2
- initialize method not called in production mode HOT 1
- show and edit are unauthorized on authorized (:manage) object HOT 1
- Called user model an alternative name
- PROJECT DISCONTINUED. STOP CREATING ISSUES AND GOTO CANCANCAN INSTEAD HOT 5
- lear
- CanCan::AuthorizationNotPerformed in AdminPanel::DashboardController#index HOT 1
- CanCan::AccessDenied exception HOT 1
- param is missing or the value is empty: activities HOT 1
- Role Infinite loop HOT 1
- CanCan is not allowing to initialize 2 users to have access to resources. Only one works at a time HOT 1
- Is CanCan being actively developed? HOT 2
- Question: How to load through parent of nested HOT 1
- How to differentiate permissions between admin/users_controller and users_controller
- Different exception messages for same pair of actions and subject
- Ability with conditions array behaving strangely for single objects
- before_filter deprecated
- Not loading expected results on :index action
- Update load_and_authorize_resource! "routing error before_filter" HOT 1
- Undefined Method exception when block is passed with curly braces
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cancan.