【问题标题】:How-to: Devise after_sign_up_redirect?操作方法:设计 after_sign_up_redirect?
【发布时间】:2011-07-03 23:07:34
【问题描述】:

我尝试按照此处 (GitHub Devise Wiki) 的说明进行操作,但对我不起作用。

我正在尝试让 Devise 在用户注册后重定向到 /welcome。我创建了一个 Registrations 控制器,但它永远不会进入视图。我在这里做错了什么?

红宝石 1.9.2 导轨 3.1.0.rc4 设计 1.4.2

谢谢

更新:

由于某种原因,after_sign_up_path_for 未被触发,但 after_sign_in_path_for 被触发。我仍然想让 after_sign_up_path_for 工作

_app/controllers/registrations_controller.rb_

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    welcome_path
  end
end

这是我的 config/routs.rb

MyApp::Application.routes.draw do
  devise_for :users

  root :to => 'pages#home'

  match "/users", :to => "users#all"
  match "/users/:id", :to => "users#show", :as => :user

  match "/welcome", :to => "users#welcome", :as => :user

  devise_for :users, :controllers => { :registrations => "registrations" } do
    get "/login", :to => "devise/sessions#new"
    get "/register", :to => "devise/registrations#new"
    get "/logout", :to => "devise/sessions#destroy"
    get '/account' => 'devise/registrations#edit'
  end
end

rake 路线的输出

new_user_session GET    /users/sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
user_session POST   /users/sign_in(.:format)               {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)              {:action=>"destroy", :controller=>"devise/sessions"}
  user_omniauth_callback        /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
           user_password POST   /users/password(.:format)              {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)          {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)         {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)              {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)                {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)                       {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)               {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)                  {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)                       {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)                       {:action=>"destroy", :controller=>"devise/registrations"}
                    root        /                                      {:controller=>"pages", :action=>"home"}
                   users        /users(.:format)                       {:controller=>"users", :action=>"all"}
                    user        /users/:id(.:format)                   {:controller=>"users", :action=>"show"}
                    user        /welcome(.:format)                     {:controller=>"users", :action=>"welcome"}
                   login GET    /login(.:format)                       {:controller=>"devise/sessions", :action=>"new"}
                register GET    /register(.:format)                    {:controller=>"devise/registrations", :action=>"new"}
                  logout GET    /logout(.:format)                      {:controller=>"devise/sessions", :action=>"destroy"}
                 account GET    /account(.:format)                     {:controller=>"devise/registrations", :action=>"edit"}
        new_user_session GET    /users/sign_in(.:format)               {:action=>"new", :controller=>"devise/sessions"}
                         POST   /users/sign_in(.:format)               {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)              {:action=>"destroy", :controller=>"devise/sessions"}
  user_omniauth_callback        /users/auth/:action/callback(.:format) {:action=>/(?!)/, :controller=>"devise/omniauth_callbacks"}
                         POST   /users/password(.:format)              {:action=>"create", :controller=>"devise/passwords"}
                         GET    /users/password/new(.:format)          {:action=>"new", :controller=>"devise/passwords"}
                         GET    /users/password/edit(.:format)         {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)              {:action=>"update", :controller=>"devise/passwords"}
                         GET    /users/cancel(.:format)                {:action=>"cancel", :controller=>"registrations"}
                         POST   /users(.:format)                       {:action=>"create", :controller=>"registrations"}
                         GET    /users/sign_up(.:format)               {:action=>"new", :controller=>"registrations"}
                         GET    /users/edit(.:format)                  {:action=>"edit", :controller=>"registrations"}
                         PUT    /users(.:format)                       {:action=>"update", :controller=>"registrations"}
                         DELETE /users(.:format)                       {:action=>"destroy", :controller=>"registrations"}

【问题讨论】:

  • after_inactive_sign_up_path_for 和 after_sign_up_path_for 在 application_controller 中都不起作用。有解决办法吗?

标签: ruby-on-rails routing devise


【解决方案1】:

您的 routes.rb 中有两次 devise_for :users。修改 routes.rb 如下:

MyApp::Application.routes.draw do
  devise_for :users, :controllers => { :registrations => "registrations" } do
    get "/login", :to => "devise/sessions#new"
    get "/register", :to => "devise/registrations#new"
    get "/logout", :to => "devise/sessions#destroy"
    get '/account' => 'devise/registrations#edit'
  end

  root :to => 'pages#home'

  match "/users", :to => "users#all"
  match "/users/:id", :to => "users#show", :as => :user

  match "/welcome", :to => "users#welcome", :as => :user

end

如果你有激活/停用逻辑,你必须覆盖after_inactive_sign_up_path_for,正如设计维基中提到的那样。你能告诉注册后它被重定向到哪里吗?

after_sign_in_path_for工作的原因可能是你有相同的sessions_controller和不同的registrations_controller,所以登录如你所愿,注册路由被阻塞,因为你已经定义了两次,所以所有的注册请求并且动作在设计/注册而不是您的自定义注册控制器中执行。

所以删除它,改变上面提到的 routes.rb,看看会发生什么。

【讨论】:

    【解决方案2】:

    您的devise_for 看起来有点奇怪。 after_sign_up_path_for 应该可以作为 application_controller.rb 中的公共方法使用

    将您的路线文件更改为devise_for :users,并将after_sign_up_path_for 放在您的application_controller.rb

    【讨论】:

      【解决方案3】:

      您的 routes.rb 文件中有重复的 devise_for 列表。尝试删除第一个,简单的 devise_for :users,只留下第二个。

      【讨论】:

        【解决方案4】:

        在我的应用程序中,我的application_controller.rb 文件中有after_sign_in_path_for 方法。试着把它放在那里,重新启动你的服务器,看看是否有什么不同。另外,我没有在protected 下列出我的方法,而是在private 下列出。

        【讨论】:

        • 那行不通。我尝试将其作为受保护和私有。我还删除了 devise_for 部分中的 :controller 部分,什么都没有。
        【解决方案5】:

        如果您想在注册后将用户重定向到欢迎页面,听起来您只是想在成功创建用户后重定向他们。所以你的 users_controller 看起来像这样:

        class UsersController < ApplicationController
          def new  
              @user = User.new  
          end  
        
          def create  
            @user = User.new(params[:user])  
            if @user.save
              flash[:notice] = "Registration successful."  
              redirect_to welcome_path  
            else  
              render :action => 'new'  
            end  
          end
        
          def show
            @user = User.find(params[:id])
          end
        
          def edit
            @user = User.find(params[:id])
          end
        
          def update
            @user = User.find(params[:id])
            if @user.update_attributes(params[:user])
              flash[:notice] = "Successfully updated user."
              redirect_to @user
            else
              render :action => 'edit'
            end
          end
        end
        

        【讨论】:

        • 由于我使用的是 Devise,所有用户创建都是通过它处理的,它提供了一种在注册、登录、注销等之后进行重定向的机制。尽管遵循文档并不适用我出于某种原因。
        【解决方案6】:

        说明对我来说效果很好(新的注册控制器,修改 routes.rb 并将注册视图复制到 app/view/registrations),但我需要稍微更改我的 routes.rb 以便获取注册控制器。顺序很重要,因为将使用第一个匹配路由 - 您希望它成为新的注册路由。

        devise_for :users, :controllers => { :registrations => "registrations" }

        devise_for :users

        (确保 devise_for :users 出现在新的注册路径之后)

        https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L82

        【讨论】:

          猜你喜欢
          • 2023-01-25
          • 1970-01-01
          • 2017-03-07
          • 1970-01-01
          • 1970-01-01
          • 2011-03-08
          • 1970-01-01
          • 1970-01-01
          • 2010-10-25
          相关资源
          最近更新 更多