Harshini
Harshini

Reputation: 73

Generating Devise Controllers - Rails Devise

I am trying devise for the first time.I am following the link:https://github.com/plataformatec/devise. Here,i have executed the command:

rails generate devise MODEL

when i have executed this,the model and view parts are created.When i checked the routes,I have noticed that there is a controller created with the name:MODEL.but i didnot find the controller in the project.My query is how can we find whether a controller is generated or not and use that controller in the project. Thanks in advance.

Upvotes: 1

Views: 19189

Answers (4)

Jin Lim
Jin Lim

Reputation: 2152

Short answer.

rails generate devise:controllers users

Upvotes: 2

Tom_soup
Tom_soup

Reputation: 58

running

rails generate devise MODEL

when using the Devise gem will not create the controllers for you.

In your case, if you want to change any methods in the Devise controllers, you may want to create your own controller that inherits Devise controllers.

For example, changing the devise registration controllers to allow first and last name would require you to create your own controller under app/controllers/MODEL/registrations_controller.rb

Link to Devise controllers here

class MODEL::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
  end
end

and instructing your routes.rb to use the controller

devise_for :MODEL, :controllers => { :registrations => "MODEL/registrations" }

Upvotes: 2

Milind
Milind

Reputation: 5112

There are many commands that devise provides..

  1. rails generate devise:install - will create config/devise.rb
  2. rails generate devise User - will create db/migration-file-for-user-model which commented code,which you can uncomment,if you need other modules..such as confirmable,omniauth etc
  3. rails generate devise:views - will create all views in the devise folder with app/views/devise/sessions,app/views/devise/confirmations,registrations etc folder and its views respectively.
  4. rails generate devise:views users - will create folder of app/views/users/passwords/,app/views/users/confirmations ..etc instead of devise folder above.
  5. rails generate devise:controllers - will create all controllers..similar to above...here it will create app/controllers/devise/sessions_controller.rb with default commented code,which you need to edit else leave it as it is for basic authentication.Moreover,you can also add users scope in the end,to generate controllers in controllers/users/ and not controllers/devise/

You may also go through this good devise tutorial ..

Hope it helps.

Upvotes: 19

puneet18
puneet18

Reputation: 4427

Replace MODEL with User like

rails generate devise User

It will generate User model under app/modeld/user.rb and user controller under app/controllers/users_controller.rb

run migration to add user table under database using command:

rake db:migrate

Upvotes: 0

Related Questions