I'm currently working on my personal website.
By adding devise gems to my code, I'va made a mistake.
I've written :
rails generate devise MODEL
and I want :
rails generate devise User
I know I could just start over, but I want to know first if it is possible to change this and if it is, how could I do that ?
Many thanks,
Raphaël.
Remove the table:
rake db:rollback VERSION=versionNumberOfMigration
Remove the configuration:
rails destroy devise:install
Remove your User model:
rails destroy devise MODEL
Check the references to devise in your routes.rb, controllers and views.
Also check for the following code snippets in your project:
devise_for (routes.rb)
before_action :authenticate_MODEL! (controllers)
MODEL_signed_in? (controllers, views) current_MODEL
MODEL_session (controllers, views)
Have you tried
rails destroy devise MODEL
Related
Simple question.
I'm using Rails 4.1.4 and Devise 3.3.0 for my app.
I'm trying to generate Devise's controllers so I can override some behaviour.
Documentation says to run...
rails generate devise:controllers [scope]
... to generate controllers under app/controllers/scope so you can then modify them. But when I run the previous command it keeps saying that there is no generator devise:controllers:
Could not find generator devise:controllers.
Does anyone knows why?.
Thanks.
UPDATE
In fact, when I run...
rails generate
... to retrieve a list of the available generators, I get the following output for Devise generators:
Devise:
devise
devise:install
devise:views
So definitelly, the devise:controllers generator isn't there. Is there a way to add it?. How?.
Thanks.
SOLVED
I've just created the controller manually and make it inherit from Devise. For example:
class Users::RegistrationsController < Devise::RegistrationsController
# Override the action you want here.
end
This controller should live in app/controllers/users/registrations_controller.rb. If you have any other scope just go with app/controllers/scope/registrations_controller.rb. For example if you have an admin scope it would be app/controllers/admins/registrations_controller.rb.
Best.
UPDATE
Following the comment from blushrt, I forgot to mention that it is important to modify config/routes.rb to make Devise use the created controller for the specific resource. For example, for users, you should put in your config/routes.rb:
devise_for :users, controllers: { registrations: "users/registrations" }
That's it. Best.
To answer the OP's original question of "Does anyone knows why?"
The problem is that this generator is currently only available on Devise's master branch, as stated on this GitHub issue.
If you want to use this feature before it's published, you could add this to your Gemfile:
gem 'devise', git: 'https://github.com/plataformatec/devise'
https://github.com/plataformatec/devise/wiki/Tool:-Generate-and-customize-controllers
You can run this command in your terminal.
bash <(curl -s https://raw.githubusercontent.com/foohey/cdc/master/cdc.sh)
Putting this here in case anyone else has this (silly) problem. I couldn't work out why a POST to /users kept routing to Devise::RegistrationsController#create rather than Users::RegistrationsController#create
The reason?
I had a typo in routes.rb
devise_for :users, controllers: { registations: 'users/registrations }
Note I had registations instead of registrations
So be wary you may have a typo in your route somewhere
I have read all stackoverflow posts and the document on https://github.com/plataformatec/devise concerning the scoped Devise views in a Rails application.
I have a single model Admin. Later I am planning to add other models such as User. My problem is that my scoped views do not work. Here is what I have done:
I modified the file config/initializers/devise.rb: added config.scoped_views = true.
Then I generated a session view (using rails g devise:views -v sessions) new.html.erb, modified it and put this file in the folder app/views/admins/sessions.
I restarted the Rails server and followed the http://0.0.0.0:3000/admin/sign_in.
Nothing changed.
Then I put the file new.html.erb into the folder app/views/admins/sessions/new, again no effect.
Additional info: routes.rb contains devise_for :admin.
Does anyone have idea what I am missing?
The route should be
devise_for :admins
If you want to keep the route, Change the view folder to app/views/admin/
I'm trying to follow this tutorial about adding a username field to Devise: http://asciicasts.com/episodes/210-customizing-devise
However, when I edit the view files new.html.erb I don't see any changes take place when I refresh the signup/login page...
What would be causing this?
Did you do:
rails g devise:views
or
rails g devise:views MyModel
When using only only model, you need to use the former version to generate the correct views. Or rename your app/views/my_models to app/views/devise
Is there a way to generate a scaffold with the admin folder prefix..For example I want one Admin folder and a few controllers for that admin folder. I want to do a scaffold for each controller in the admin folder and i wanted to know if there was something like
script/generate scaffold admin:something somefield:string
The scaffold generator can take a namespaced argument:
# Rails 3
rails g scaffold Admin::Something somefield:string
# Rails 2
script/generate scaffold Admin::Something somefield:string
Note, that if you'd like to leave model unprefixed, you have to edit some of generated files by hand to implement it. That is why I created rails-admin-scaffold gem and wrote detailed article about custom admin area scaffolding. If that's already not relevant for you, maybe it'll help someone else.
I'm using Devise as the authentication mechanism for my app.
I want to add additional information to my user model, like User statistics, profile pic , and other relevant information about the user.How to achieve this?.
Even after creating "rails g devise : views" it creates only the views which I can customize it doesn't give me of its controllers to edit.
Else can I edit the devise gem itself to fit my requirements ?
I have never tried to customize a GEM. Any links to start with customizing a existing gem?
Devise itself is a Rails engine and you can override any of its functionality by creating a copy of the file you wish to change in your local directory. When Rails begins to look for an appropriate controller for a request, it will first check the local application, then vendor/gems, and then the loaded gems themselves.
In the case of Devise, they mention that modifying controllers should be done in this way:
Configuring controllers
If the customization at the views level is not enough, you can customize each controller by following these steps:
1) Create your custom controller, for
example a Admins::SessionsController:
class Admins::SessionsController < Devise::SessionsController
end
2) Tell the router to use this controller:
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
3) And since we changed the controller, it won’t use the "devise/sessions" views, so remember to copy "devise/sessions" to "admin/sessions".
Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate.
Here is the source of the quote: https://github.com/plataformatec/devise