Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How do I override Devise controller and Devise routes at the same time?

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

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

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 )

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
like image 285
laman Avatar asked Feb 27 '14 08:02

laman


1 Answers

There are several potential issues you may have:


Skip

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

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


Definition

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

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

like image 180
Richard Peck Avatar answered Oct 23 '22 19:10

Richard Peck