CanCan, Devise and MongoMapper #63

Closed
rurounijones opened this Issue Apr 28, 2010 · 4 comments

Projects

None yet

2 participants

@rurounijones

Hoi thar,

I have just about got CanCan working (after a few trip ups I must admit) but there is one problem left that I cannot figure out. I am using CanCan, Devise and MongoMapper. (Rails 2.3.5)

I am trying to say that only an admin can create a user.

class UsersController < ApplicationController
  load_and_authorize_resource

  def new
    @user = User.new
  end

My Ability class looks like this

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # Non-logged in user

    if user.role? :admin
      can :manage, :all
    else
      can [:show, :edit, :update], User do |suser|
        suser.try(:id) == user.id
      end
    end
  end
end

However, no matter who I am logged in as, when I try to go to the New User action I get the following error and tracelog http://gist.github.com/381779 (Sorry I think I included the three tracelogs, one for each option, just the top one will be interesting to you I think )

@rurounijones

A workaround is to do this:

class UsersController < ApplicationController
  load_and_authorize_resource :except => :new

  def new
    @user = User.new
    authorize! :new, @user
  end
@ryanb
Owner
ryanb commented Aug 3, 2010

I think the problem is MongoMapper does not allow nil to be passed in as an argument to User.new. This should be a fairly easy fix. Thanks for reporting.

@ryanb
Owner
ryanb commented Aug 5, 2010

don't pass nil to 'new' call when no params are specified - closed by 67b0695

@rurounijones

Groovy, thank you very much! and another thank you for the railscasts.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment