Question

Environment: RAILS 3.2 + DEVISE for auth + Invitable + Confirmable add-ons.

Using devise (2.2.3) 
Using devise-i18n (0.6.5) 
Using devise_invitable (1.0.3) 

I am trying to redirect to a specific location after ACCEPT (TO SIGN UP), but only after_sign_in_path_for seems to be called after SIGN IN and ACCEPT.

I haven't been able to have after_accept_path_for working. It continues to redirect to the "after sign in" location.

HERE THE CODE

In my routes.rb:

devise_for :users, 
     :controllers => { :registrations => 'registrations', :invitations => 'invitations' }

rake routes give me this:

           new_user_session GET    /users/sign_in(.:format)                          devise/sessions#new
               user_session POST   /users/sign_in(.:format)                          devise/sessions#create
       destroy_user_session DELETE /users/sign_out(.:format)                         devise/sessions#destroy
              user_password POST   /users/password(.:format)                         devise/passwords#create
          new_user_password GET    /users/password/new(.:format)                     devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)                    devise/passwords#edit
                            PUT    /users/password(.:format)                         devise/passwords#update
   cancel_user_registration GET    /users/cancel(.:format)                           registrations#cancel
          user_registration POST   /users(.:format)                                  registrations#create
      new_user_registration GET    /users/sign_up(.:format)                          registrations#new
     edit_user_registration GET    /users/edit(.:format)                             registrations#edit
                            PUT    /users(.:format)                                  registrations#update
                            DELETE /users(.:format)                                  registrations#destroy
          user_confirmation POST   /users/confirmation(.:format)                     devise/confirmations#create
      new_user_confirmation GET    /users/confirmation/new(.:format)                 devise/confirmations#new
                            GET    /users/confirmation(.:format)                     devise/confirmations#show
     accept_user_invitation GET    /users/invitation/accept(.:format)                devise/invitations#edit
            user_invitation POST   /users/invitation(.:format)                       devise/invitations#create
        new_user_invitation GET    /users/invitation/new(.:format)                   devise/invitations#new
                            PUT    /users/invitation(.:format)                       devise/invitations#update

In my registration controller:

class RegistrationsController < Devise::RegistrationsController
  # clear session
  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  #protected
  # after_sign_up_path_for doesn't seem to be called when using Confirmable module
   # def after_inactive_sign_up_path_for(resource)
   #   #me_path
   #   session[:user_return_to].nil? ? me_path : session[:user_return_to]
   # end

  private

  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end

Also

class Users::InvitationsController < Devise::InvitationsController
  protected 
  def after_accept_path_for
    session[:user_return_to].nil? ? me_path : session[:user_return_to]
  end
end

In my application controller, (I left intentionally some commented code I tried to make it work):

  def store_location
      session[:user_return_to] = request.fullpath
  end

  #   def after_sign_up_path_for
  #     me_path
  #   end 
  #   
  #   def after_inactive_sign_up_path_for(resource)
  #     me_path
  #     #session[:user_return_to].nil? ? me_path : session[:user_return_to]
  #   end

  # https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
  def after_sign_in_path_for(resource)
    me_path
    #dashboard_path
    #session[:user_return_to].nil? ? dashboard_path : session[:user_return_to]
  end

Any suggestions?

ADDED DEBUGGIN REDIRECTS

Add this to my application_controller

  def redirect_to_with_logging(*args) 
    logger.debug "Redirect: #{args.inspect} from #{caller[0]}" 
    redirect_to_without_logging *args 
  end 

  alias_method_chain :redirect_to, :logging

After Sign in, works like a charm

Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-25 14:20:04 +0200
Processing by Devise::SessionsController#create as HTML
    [... I removed some of the Session creation info ...]
Redirect: ["/dashboard"] from /Users/joel/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.11/lib/action_controller/metal/responder.rb:135:in `redirect_to'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 968ms (ActiveRecord: 0.0ms)

DOCS:

After sign in

After sign up

After Accept <====

Was it helpful?

Solution

Override after_accept_path_for in Invitations controllers

class Users::InvitationsController < Devise::InvitationsController
  protected 
  def after_accept_path_for(resource)
    me_path
  end
end

IMPORTANT:

put this file in 'controllers/users' directory

Fix the routes.rb to use the Users::InvitationsController

devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }

OTHER TIPS

You may put redirect_to my_specific_url at the end of sign_up controller's method, to redirect after signing up

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top