Generating Devise Controllers - Rails Devise - ruby-on-rails

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.

There are many commands that devise provides..
rails generate devise:install - will create config/devise.rb
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
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.
rails generate devise:views users - will create folder of app/views/users/passwords/,app/views/users/confirmations ..etc instead of devise folder above.
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.

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

Short answer.
rails generate devise:controllers users

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

Related

Adding custom callback to user model with devise

I'm trying to add this callback to my User model, which I generated using Devise.
before_save :check_invite_code
def check_invite_code
if self.invite_code == 'first20'
User.save
else
{icon: 'error', message: "Sorry that's not a valid invite code"}
end
end
The issue I am having is with passing the returned hash with the else block back to my view. Typically I'd be able to use the icon and message in the flash in my controller. I'm not sure how to do that. I don't have a UsersController because devise takes care of a the routing with the path being devise/controller#action. So do I create a devise directory inside of controllers, then create the corresponding controllers like sessions, etc and override the devise methods? Looking for some guidance from someone with devise experience.
If you want to customize the UserController you can easily do that by
rails generate devise:controllers [scope]
For example
rails generate devise:controllers users
Check out the documentation here

Setting up automatic password with Devise

I have setup Devise, I have set up the below code in my user.rb file
def self.create_auto_password
generated_password = Devise.friendly_token.first(8)
self.create(password: generated_password, password_confirmation: generated_password)
end
How can I get into the Devise registration controller to then call my new method which I have set up to auto generate a password?
You can access the devise controllers after installing Devise by running the following on your command line:
rails generate devise:controllers
This will generate a 'Devise' folder in your controllers folder. You can then modify the controllers as needed. It sounds like you would like to modify the registration controller to add the auto-setting password.
You can find more docs on their .git page here: https://github.com/plataformatec/devise

Where can I find devise controller?

I'm trying to apply this post ( Devise update user without password ) for users don't need to insert password to update informations.
But, I'm very confused where is this controller. Devise don't create any controller in my app/controller folder. I search in all the folders but I cant find.
Where I that controller?
I see posts talking about create a new controller, but I just want to modify little things.
You don't edit (or shouldn't) the Devise controllers. Instead you create your own controller and inherent from the Devise controller.
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def update
# add custom update logic here
end
end
Notice how RegistrationsController inherets from Devise::RegistrationsController. Now you can overide the registration methods (or modify them and call super). Even the page you are referencing about overriding the devise default behavior relies on class inheritance.
JTG offers good advice, you shouldn't edit the gem directly. As a more direct answer to the question:
To print the folder of your gem:
$ bundle show devise
To open the gem in a text editor ( while being sure not to change anything )
$ bundle open devise

Ruby on Rails' gem "Devise" doesn't even have a controller file in app/controllers?

After installing Devise, there are routes to
/users/sign_in
/users/sign_up
/users/sign_out
but there is no file app/controllers/users_controller.rb? why does this require no controller file or is it just some where else?
The controller file is located within the gem, and you don't need to write one yourself. Most everything can be done via configuration. See https://github.com/plataformatec/devise#readme for more specific details.
You can, however, generate the views so that you may override them yourself:
rails generate devise:views
That will place files in app/views/devise that you can modify for all of the forms, etc. that Devise provides.
It's bundled with the gem. You can generate a controller 'User' separately with further actions:
class UserController < ApplicationController
def show
#user = current_user
end
end

I want to customise devise gem's controllers, is it possible and how to do?

Is there a way to customise the devise controllers , as we can modify the devise views using the "rails g devise:views" generator command. ??
Ok purpose here is to create a statistics table's row for the current user as soon as a user is registered.
I have a user statistics maintained for every user.I just want to trigger the create method of the userstats controller in the background when a user sign-up for my web app.
Is there a way to do this ?
You need to create your own controllers inheriting from Devise's.
class Admins::SessionsController < Devise::SessionsController
end
Then you tell devise to use that controller:
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
And copy your views from devise/sessions, to admin/sessions.
You can read it here: https://github.com/plataformatec/devise
Or simply do this:
rails generate devise:controllers Admin
or copy the devise controllers from where they are now to your app. This is what I did with RVM:
cp -R ~/.rvm/gems/ruby-1.9.3-p194#my_gemset/gems/devise-2.1.0/app/controllers/* my_rails_app/app/controllers/

Resources