Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access Devise controllers?

Are controllers in devise automatically generated? How do you access them?

I know for views you do rails generate devise_views.

like image 218
katie Avatar asked Jun 04 '11 01:06

katie


People also ask

What does devise Authenticate_user do?

This allows all users to access the index route, but only authenticated users could access other routes in the comments controller. Note that :authenticate_user! is a method provided by Devise, so if you're using Bcrypt, you'll have to create your own custom method.

How does devise gem work?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


2 Answers

Under the assumption that you want to see these controllers in order to modify or override them, Devise now provides a simple generator which recreates their controllers in your app to make this easy. As per the documentation (which will be most up-to-date):

1) Create your custom controllers using the generator which requires a scope:

console

rails generate devise:controllers [scope] 

If you specify users as the scope, controllers will be created in app/controllers/users/. And the sessions controller will look like this:

class Users::SessionsController < Devise::SessionsController   # GET /resource/sign_in   # def new   #   super   # end   ... end 

2) Tell the router to use this controller:

devise_for :users, controllers: { sessions: "users/sessions" } 

3) Copy the views from devise/sessions to users/sessions. Since the controller was changed, it won't use the default views located in devise/sessions.


4) Finally, change or extend the desired controller actions.

You can completely override a controller action:

class Users::SessionsController < Devise::SessionsController   def create     # custom sign-in code   end end 

Or you can simply add new behaviour to it:

class Users::SessionsController < Devise::SessionsController   def create     super do |resource|       BackgroundWorker.trigger(resource)     end   end end 

This is useful for triggering background jobs or logging events during certain actions.

Remember that Devise uses flash messages to let users know if sign in was successful or unsuccessful. Devise expects your application to call flash[:notice] and flash[:alert] as appropriate. Do not print the entire flash hash, print only specific keys. In some circumstances, Devise adds a :timedout key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.

like image 54
Erik Trautman Avatar answered Oct 14 '22 12:10

Erik Trautman


Devise uses internal controllers, which you can access and subclass in your own code. They are under the Devise module. For example, to extend the RegistrationsController:

class MembershipsController < Devise::RegistrationsController   # ... end 

Then all you have to do is configure Devise's routes to use your controller instead:

devise_for :members, :controllers => { :registrations => 'memberships' } 
like image 34
Matheus Moreira Avatar answered Oct 14 '22 11:10

Matheus Moreira