使用 Devise 登录后重定向

Redirecting after sign in with Devise

我知道这个问题已经被问过很多次了,但不知何故它对我不起作用。我希望用户在登录后被重定向到特定页面(使用 Devise)。我正在使用 Rails 3.2 和 Ruby 4.

因为它是用户被重定向到: - 登录后,如果用户从根路径所属的控制器控制的页面之一进入登录页面,它们将被重定向到根目录。这些页面不需要授权。 - 登录后,如果用户通过单击需要授权的页面自动进入登录页面,他们将被重定向到请求的页面。

要在登录到特定页面后重定向用户,我知道您需要向控制器添加一些行。对于控制需要授权的页面的控制器,我添加了:

class AccountsController < ApplicationController
    before_filter :authenticate_user!

    def user1_home
    end
    def user2_home
    end
    def user3_home
    end

    protected
    def after_sign_in_path_for(resource)
        'accounts/user1_home'
    end
end

然而,这并没有改变任何东西。我也用 def after_sign_in_path_for(resource) 'accounts#user1_home_path' end

试过了

我是否也应该更改 routes.rb 中的某些内容? 我的路线是:

        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)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/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
                     toc GET    /toc(.:format)                    pages#toc
             stakeholder GET    /stakeholder(.:format)            pages#stakeholder
                   about GET    /about(.:format)                  pages#about
   doelgroep_onderneming GET    /doelgroep_onderneming(.:format)  pages#doelgroep_onderneming
           doelgroep_ngo GET    /doelgroep_ngo(.:format)          pages#doelgroep_ngo
            doelgroep_se GET    /doelgroep_se(.:format)           pages#doelgroep_se
             partnership GET    /partnership(.:format)            pages#partnership
                 contact GET    /contact(.:format)                pages#contact
     account_stakeholder GET    /account/stakeholder(.:format)    accounts#stakeholder_home
        account_business GET    /account/business(.:format)       accounts#business_home
           account_admin GET    /account/admin(.:format)          accounts#admin_home
                    root        /                                 pages#index

您绝对不应该在帐户控制器中为每个用户定义方法。

Devise 在 github 上提供了完成此操作的说明:https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

您需要将 after_sign_in_path_for 移动到 ApplicationController class 而您不应该 'accounts/user1_home'。要为每次使用转到右侧 url,您应该有一些类似于 user_path(@user) 的内容,但我没有足够的代码来确切地知道要使用什么。您应该 运行 在控制台 window 中使用 rake routes 命令来查看您提供的路由。在以下位置阅读有关路由的更多信息:http://guides.rubyonrails.org/routing.html

如果这是他们在登录之前尝试访问的经过身份验证的页面,您想要重定向到,那么您只需在您的 ApplicationController 中这样做:

def after_sign_in_path_for(resource)
  if session[:user_return_to] == nil
    your_actual_next_path
  else
    super
  end
end

是的。在任何时候(登录前或登录后)设置以下内容:

session["user_return_to"] = request.original_url # Will redirect the user to where they came from