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

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

问题描述

我使用 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.

我用谷歌搜索并搜索 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

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

这将为您提供所需的路由,并将路由到您的应用程序中的经过身份验证的"索引页面,从而显示 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:如何同时覆盖设计控制器和设计路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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