Rails:如何同时覆盖Devise控制器和Devise路线? [英] Rails: How do I override Devise controller and Devise routes at the same time?

查看:91
本文介绍了Rails:如何同时覆盖Devise控制器和Devise路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Rails 4.0.2 and Devise 3.2.2 to handle user registration / authentication.

我已经在google上搜索了stackoverflow以获取答案,但实际上无法找到可以回答我问题的东西。

I have googled and search stackoverflow for answers, can't really find something that can answer my question.

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

The below code is my routes.rb, I have skip all sessions routes and registration routes but for some reason, Devise is not using my custom registrations_controller.rb because if it is, it should redirect to /pages/success (please see below my registrations_controller.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



registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

protected

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

end


推荐答案

您可能会遇到一些潜在的问题:

There are several potential issues you may have:

跳过

如果重新跳过注册功能,我想这会阻止Devise调用您的 RegistrationsController

If you're skipping the registrations functionality, I'd imagine it would prevent Devise from calling your RegistrationsController?

我个人这样做(更正您的路由):

I would personally do this (correct your routes):

#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'}

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

This will give you the routes you need, and will route to the "authenticated" index page in your app, thus either showing the login or registration page for Devise

定义

您可能遇到的另一个问题是设备注册控制器的定义不正确。我们在一个最新的开发应用程序中使用以下代码:

The other issue you may have is an incorrect definition of your Devise Registrations controller. We use this code in a very recent development app:

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

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

Perhaps you could try using the :: before your Devise::RegistrationsController to see if it calls?

这篇关于Rails:如何同时覆盖Devise控制器和Devise路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆