DEVISE after_inactive_sign_up_path_for未被调用

时间:2013-04-23 10:21:52

标签: ruby-on-rails devise devise-invitable

环境:RAILS 3.2 + DEVISE for auth + Invitable + Confirmable附加组件。

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

我尝试在接受(注册)后重定向到特定位置,但在登录和接受后似乎只调用after_sign_in_path_for

我无法让after_accept_path_for工作。 它继续重定向到“登录后”位置。

这里的代码

在我的routes.rb中:

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

rake routes给我这个:

           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

在我的注册控制器中:

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

另外

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

在我的应用程序控制器中,(我故意留下了一些我试图使其工作的注释代码):

  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

有什么建议吗?

添加调试重定向

将此添加到我的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

登录后,就像魅力一样

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&lt; ====

2 个答案:

答案 0 :(得分:2)

在邀请控制器中覆盖after_accept_path_for

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

重要:

将此文件放在'controllers / users'目录

修复routes.rb以使用Users::InvitationsController

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

答案 1 :(得分:0)

您可以将redirect_to my_specific_url放在sign_up控制器方法的末尾,以便在注册后重定向