2

环境: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) 

我试图在接受(注册)之后重定向到特定位置,但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)

文档:

登录后

注册后

接受后 <====

4

2 回答 2

2

在邀请控制器中覆盖 after_accept_path_for

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

重要的:

将此文件放在“控制器/用户”目录中

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

devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }
于 2013-04-25T22:08:43.243 回答
0

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

于 2013-04-25T01:08:35.050 回答