Witam!
Mam problemy z ustawieniem Rails Admin oraz CanCan.
Na poczatku zainstalowalem gem Rails Admin, nastepnie chcialem dodac CanCan aby moc stworzyc hierarchie userow.
Wygenerowalem plik Ability
Stworzylem dwie tabele w bazie.
Roles oraz UsersHaveAndBelongToManyRoles.
Dodalem do modeli
[code:ruby]# User Model
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
…
Role model
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end[/code]
I moglem z panelu dodawac do userow role wpisujac jakas nazwe roli.
Do modelu user.rb dodalem
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
Stworzylem tez app/controllers/users/registrations_controller.rb
gdzie dodalem
[code:ruby]class Users::RegistrationsController < Devise::RegistrationsController
before_filter :check_permissions, :only => [:new, :create, :cancel]
skip_before_filter :require_no_authentication
def check_permissions
authorize! :create, resource
end
end[/code]
W routes dodalem
devise_for :users, :controllers => { :registrations => "users/registrations" }
A w application controller
class ApplicationController < ActionController::Base
...
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to root_url
end
...
end
Niestety cos nie dziala. Po probie zalogowania wywala mnie na strone glowna.
[code:ruby]class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :superadmin
can :manage, :all
elsif user.role? :adsadmin
can :manage, [Ad, User]
elsif user.role? :adsteam
can :access, :rails_admin, :read, [Ad]
# manage products, assets he owns
can :manage, Ad do |ads|
ads.try(:owner) == user
end
can :manage, User do |user|
user.assetable.try(:owner) == user
end
end
end
end[/code]
Niestety teraz zostaje przekierowany na strone glowna po odswierzeniu wywala blad
wrong number of arguments (4 for 3)
Rails.root: /home/nikos/Documents/sites/ads_portal
Application Trace | Framework Trace | Full Trace
app/models/ability.rb:12:in can' app/models/ability.rb:12:in
initialize’
Pozdrawiam