1
votes

I am trying to get Cancan to work with Devise on my Rails application. I tried to follow the directions here to set it up:

http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

I am stuck as I am getting the following error:

ArgumentError in Users::RegistrationsController#new wrong number of arguments (2 for 1)

app/models/ability.rb:7:in initialize' app/controllers/users/registrations_controller.rb:6:incheck_permissions'

My ability.rb reads as follows:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user

    if user.role? :super_admin
      can :manage, :all
    elsif user.role? :admin
      can :manage, :all
    elsif user.role? :user
      can :read, :all
      # manage products, assets he owns
      can :manage, Product do |product|
        product.try(:owner) == user
      end
      can :manage, Asset do |asset|
        asset.assetable.try(:owner) == user
      end
    end
  end
end

My registrations controller reads:

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

This only occurs on the signup page. All other pages within the app are working fine.

1

1 Answers

0
votes

When using cancan u have to use following line in your controller.

load_and_authorize_resource