10

I have looked all over the place, and found a lot of info... but nothing works for me and I don't get it :(

I know that you are suppose to override the registration controller, like this:

class Users::RegistrationsController < Devise::RegistrationsController

def after_sign_up_path_for(resource)
  authors_waiting_path
end 

end

Then following the example showed by Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, I am supposed to change my routes to update the access the new controller:

devise_for :users, :controllers => { :registrations => "users/registrations" } do
#get '/author/sign_up', :to => 'devise/registrations#new'
#get '/client/sign_up', :to => 'devise/registrations#new'  
get '/author/sign_up', :to => 'users/registrations#new'
get '/client/sign_up', :to => 'users/registrations#new'      
end

Yes, I have something a bit strange here, because I am catching some specific path to send them to the registration page, this allows me to create effectively 2 registration scenario. I commented what I had before I had overridden the registration controller.

Even with all this and my authors_waiting_path being a valid path, it just keeps on going to the sign-in page after registration :(

This is really frustrating.

Alex

edit: I also found this on the devise wiki: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after-registration-(sign-up)

But I have no idea where to define this create method ? should I override the session controller ???

edit 2:

I put a dummy override of the controller:

  class Pouets::RegistrationsController < Devise::RegistrationsController

    def after_sign_up_path_for(resource)
      authors_waiting_path
    end 

    def new
      super
    end

    def create
      puts "was here"
      super
    end

    def edit
      super
    end

    def update
      super
    end

    def destroy
      super
    end

    def cancel
      super
    end

  end

And I never the "was here" in my logs.... I really have the feeling that it's totally ignoring the override... I must be doing something wrong :(

Viren
  • 5,812
  • 6
  • 45
  • 98
Alex
  • 4,367
  • 5
  • 29
  • 45

2 Answers2

9

Ok... I am able to override it so you should be either :0

Create folder app/controllers/users

put there registrations_controller.rb with: (option with session - but it will try sign_in and later redirect - it may be not intended behavior for you ). Furthermore this is from devise wiki and I am not sure if it works

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    session["#{resource_name}_return_to"] = complete_path
    super
  end

end

restart application (just for ensure you don't trust anything)


All in all you must override Create If you want redirect only Users... if you want define some more complex scenario you should monkeypatch sign_in_and_redirect

so your controller will looks like

class Users::RegistrationsController < Devise::RegistrationsController
  # POST /resource/sign_up
  def create
    build_resource

    if resource.save
      set_flash_message :notice, :signed_up

      #sign_in_and_redirect(resource_name, resource)\
      #this commented line is responsible for sign in and redirection
      #change to something you want..
    else
      clean_up_passwords(resource)
      render_with_scope :new
    end
  end
end

second option try to monkeypatch helper ....

module Devise
  module Controllers
    # Those helpers are convenience methods added to ApplicationController.
    module Helpers
      def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false)
        #intended behaviour for signups
      end
    end
  end
end
Chris Peters
  • 17,918
  • 6
  • 49
  • 65
Piotr Mąsior
  • 1,580
  • 1
  • 11
  • 20
  • Sorry, but I think you are confusing what I want to do, I wan to redirect avec sign UP not sign IN. I already handle the sign in process, I just want to direct my users to a waiting page while they activate their confirmation email. And yes you are right the method to override sign_in path is in the main app controller, but not the after sign up method unfortunately :) By the way, I think it should it does not make sense to have those methods all over the place – Alex Jan 24 '11 at 16:01
  • huh my bad, Are you pretty sure your code is properly hiting this controller? Maybe add some raise "Test" to def new and check if it will render correct – Piotr Mąsior Jan 24 '11 at 16:10
  • Check given solution. If you are unhappy ... try add session var after successful sign_up => "#{scope}.return_to" ... it should redirect either – Piotr Mąsior Jan 24 '11 at 16:21
  • No pb, I updated my post again, see edit 2. I really don't think it's even hitting my controller, even tough there are the routes for it and all... One thing I don't get to is, if I override, am I supposed to recreate all the methods and call super ? or juste the one I want ? Could you show me what the whole file should look like ? I am probably making some rookie assumptions here :( – Alex Jan 24 '11 at 16:22
  • Sorry, but can you explain the session var thing in a little bit more details please ? If I could I would really like not having to override the controller... thx :) and sorry for abusing your patience, I have a feeling I am getting there ! – Alex Jan 24 '11 at 16:33
  • ok edited it with session... I think that makes 3 different approaches now – Piotr Mąsior Jan 24 '11 at 16:47
  • Thanks I will try the session thing. I tried your 2nd solution, it does work, but something mysterious is happening. I get into the create via the override, it does execture my redirect_to mypath (I can see it in the log, and it complains if the path does not exist in the route) but it never gets THERE !??! Somehow in the middle of it the path is redirected to sign_in... – Alex Jan 24 '11 at 16:57
  • Thank you so much for your time piotr, your solution #2 works, the first time it did not redirect because I was asking for authentication on the user that is required to validate his emails... stupid me ! Thanks again ! – Alex Jan 24 '11 at 17:47
  • "I am able to override it so you should be either :0" -- huh? That doesn't really make sense. It's either 'I am able to override it so you should be able to as well' or 'I am *not* able to override it so you shouldn't be able to either'. – jcollum Feb 29 '12 at 20:38
  • He's probably not a native English speaker give the guy a break. – Noz Jul 17 '12 at 17:17
3

I have tried the above solution and while it works, reading devise code, I have found that all you actually need in order to sign-out just registered user and redirect is:

  1. to add is_approved or similar to your user table and
  2. to add active_for_authentication? method in your User model

Code:

class User < ActiveRecord::Base

  # ... some code

  def active_for_authentication?
    super && is_approved
  end
end

Was a bit hard to find when I needed it, but that is all. I am actually writing it here in case someone else needs it.

Krule
  • 6,468
  • 3
  • 34
  • 56
  • Very helpful, thanks for mentioning this. [Docs](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Authenticatable) – Hana Aug 08 '13 at 14:35