问题
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.
回答1:
There are many commands that devise
provides..
rails generate devise:install
- will createconfig/devise.rb
rails generate devise User
- will createdb/migration-file-for-user-model
which commented code,which you can uncomment,if you need other modules..such asconfirmable
,omniauth
etcrails generate devise:views
- will create all views in thedevise
folder withapp/views/devise/sessions
,app/views/devise/confirmations
,registrations
etc folder and its views respectively.rails generate devise:views users
- will create folder ofapp/views/users/passwords/
,app/views/users/confirmations
..etc instead ofdevise
folder above.rails generate devise:controllers
- will create allcontrollers
..similar to above...here it will createapp/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 addusers
scope in the end,to generate controllers incontrollers/users/
and notcontrollers/devise/
You may also go through this good devise tutorial ..
Hope it helps.
回答2:
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" }
回答3:
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
来源:https://stackoverflow.com/questions/41972575/generating-devise-controllers-rails-devise