Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Devise controller

I would like to customize my registrations controller for Devise in Rails. I understand that you must create a controller like this:

class AccountsController < Devise::SessionsController
  def create
    super
  end
end

Well, that's all very good. But then let's say I want to fully control what happens in my #create action. How do I do that? How do I manually create a model and pass it all the params? Would Account.create(params[:account]) handle it smoothly? Is there some internal stuff going on I should know about or is my only option to call #super inside the action?

like image 808
snitko Avatar asked Jan 11 '11 07:01

snitko


People also ask

Does devise work with Rails 7?

Our out-of-the box Devise setup is now working with Rails 7. Once again, if you'd like to refer to any of the code for this setup, or use the template wholesale for a new app, the code is available on GitHub, and you may also use it as a template repo to kick off your own Rails 7 devise projects.


2 Answers

As long as you fulfil your required fields you can call Account.create in your example, I'm pretty sure the default Devise required fields are login, password and password_confirmation

We do this in a CRUD screen for creating devise users,

@admin = Admin.new(params[:admin])
if @admin.save
  redirect_to admin_admins_path, :notice => 'New Administrator has been added'
else
  render :action => "new"
end

and you don't want to extend the Devise session controller, a normal controller extending ApplicationController is fine or you can extend Devise::RegistrationsController and overwrite the methods you want to tweak in a registrations_controller.rb file

like image 79
John Beynon Avatar answered Sep 30 '22 15:09

John Beynon


You can also have a look at the source on Github, if you want to be sure you're overriding things properly, and be sure you're not missing any processing...

https://github.com/plataformatec/devise/tree/master/app/controllers

like image 34
sands Avatar answered Sep 29 '22 15:09

sands