Saturday, April 30, 2011

Overwrite Devise Registrations Controller in Rails 3

I have a Rails application with
Rails version-3.0.5,
Ruby version- "ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]" and devise gem.

I wanted to overwrite some of the methods in Registrations Controller used by Devise. As it is done by Devise internally, I needed to create a controller that inherits from Devise::SessionsController. Here is how I did it:

Changed the routes as:
devise_for :users ,:controllers => { :registrations => "registrations" }

Added one controller named "registrations_controller" with following codes:
class RegistrationsController < Devise::RegistrationsController
  before_filter :authenticate_user!, :only => :token

  def new
      super
    end

 
  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "You have signed up successfully. If enabled, a confirmation was sent to your e-mail."
      redirect_to root_url
    else
      render :action => :new
    end
  end

  def update
    super
  end
end
'new' aciton with 'super' keyword is required. Super will executes the code from the inherited method from the Devise controller.
This is very simple 'create' action which can be customized.

3 comments:

  1. Thank you so much for this information! Totally saved my skin.

    ReplyDelete
  2. Thank you, your post direct me on right way.

    ReplyDelete
  3. Please update it according to rails 5

    ReplyDelete