Search code examples
ruby-on-railssorcery

Couldn't find User with id=pJycxPPmoBQw9D2mfW6W


Actually I'm using sorcery for authentification with the following set up :

  • rails 3.2.3
  • postgresql 9.1.3
  • cancan

When I click on the activation link I get the following error:

ActiveRecord::RecordNotFound in UsersController#activate 

Couldn't find User with id=pJycxPPmoBQw9D2mfW6W

users_controllers.rb

#cancan

before_filter :require_login, :except => [:not_authenticated, :new, :create, :activate]
load_and_authorize_resource 
skip_authorization_check :only => [:new, :create, :activate]
skip_authorize_resource  :only => [:new, :create, :activate]

#activate method

def activate
    if (@user = User.load_from_activation_token(params[:id]))
      @user.activate!
      redirect_to(login_path, :notice => 'Your account is activated.')
    else
      not_authenticated
    end
  end

generate token

def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
end

load_from_activation_token in sorcery source code

def load_from_activation_token(token)
  token_attr_name = @sorcery_config.activation_token_attribute_name
  token_expiration_date_attr = @sorcery_config.activation_token_expires_at_attribute_name
  load_from_token(token, token_attr_name, token_expiration_date_attr)
end

Can I have some help please?


Solution

  • Aha, I think the problem is here:

    load_and_authorize_resource
    

    Apparently, params[:id] for #activate request is not a user id, but a token. Cancan tries to load a user with this and (expectedly) fails. You need to skip loading for this action too (in a similar manner to how you already skip authorization).

    skip_load_and_authorize_resource  :only => [:new, :create, :activate]