如何同时覆盖Devise控制器和设计路径?

时间:2014-02-27 08:34:13

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

我正在使用Rails 4.0.2和Devise 3.2.2来处理用户注册/身份验证。

我用Google搜索并搜索stackoverflow寻找答案,无法真正找到能够回答我问题的内容。

以下代码是我的 routes.rb ,我已跳过所有会话路由和注册路由但由于某种原因,Devise未使用我的自定义 registrations_controller.rb ,因为如果是,它应该重定向到/ pages / success(请参阅下面我的registrations_controller.rb)

的routes.rb

App::Application.routes.draw do

  resources :posts
  resources :questions
  get "users/:id", to: "users#show" 

  devise_for :users, :controllers => {:registrations => "registrations"}, :skip =>     [:sessions, :registrations]


  as :user do
    get 'login' => 'devise/sessions#new', :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
  end


  as :user do
    get '/' => 'devise/registrations#new', :as => :new_user_registration
    post 'register' => 'devise/registrations#create', :as => :user_registration
  end

  get "registrations/update"
  get "pages/home"
  get "pages/privacy"
  get "pages/terms"
  get "pages/success"

end 

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

protected

  def after_inactive_sign_up_path_for(resource)
    '/pages/success'
  end

end

1 个答案:

答案 0 :(得分:13)

您可能会遇到以下几个问题:


<强>跳过

如果您正在跳过注册功能,我想它会阻止Devise调用您的RegistrationsController

我个人会这样做(纠正你的路线):

#config/routes.rb
root to: "users#index" (where ever your "logged-in" page is)

devise_for :users, path: "", controllers: { sessions: "sessions", registrations: "registrations" }, path_names: { sign_in: 'login', password: 'forgot', confirmation: 'confirm', unlock: 'unblock', sign_up: 'register', sign_out: 'signout'}

这将为您提供所需的路线,并将路由到您应用中的“经过身份验证的”索引页面,从而显示Devise的登录或注册页面


<强>定义

您可能遇到的另一个问题是您的Devise Registrations控制器的定义不正确。我们在最近的开发应用程序中使用此代码:

#app/controllers/registrations_controller.rb
class RegistrationsController < ::Devise::RegistrationsController
end

也许您可以尝试在::之前使用Devise::RegistrationsController来查看它是否在呼叫?