How to customize the default login page in ActiveAdmin? - ruby-on-rails

The Problem
I'm trying to customize the default login page in ActiveAdmin, but I'm having trouble getting the customizations to go through.
What I've tried
A commenter on this RailsCasts episode suggests copying this file to app/views/active_admin/devise/sessions/new.html.erb and customizing it from there. However, doing so does not seem to replace the default login form.
I also tried replacing devise_for :admin_users, ActiveAdmin::Devise.config with devise_for :admin_users as the commenter suggests, which seems to point the routes to the right place, but I get a bunch of server errors related to none of the ActiveAdmin variables being recognized in this context.
I've searched the docs, but I haven't been able to find documentation around customizing the login form in particular.
Question
What's the best way to go about customizing the login form in ActiveAdmin?

With the default ActiveAdmin config where the Devise resource is admin_user, your new.html.erb should go in app/views/admin_users/sessions/new.html.erb instead.
An easy way to copy out all the Devise templates is to do rails g devise:views admin_users, though it turns out ActiveAdmin comes with its own versions of these views: https://github.com/gregbell/active_admin/tree/master/app/views/active_admin/devise
If you're additionally trying to change the layout that Devise's new.html.erb is rendered with, you can copy the layout file out from ActiveAdmin into app/views/layouts/active_admin_logged_out.html.erb
The current layout file used for the login page is here:
https://github.com/gregbell/active_admin/blob/master/app/views/layouts/active_admin_logged_out.html.erb

First copy all the devise views to your app:
rails g devise:views admin_users
Second add config.scoped_views = true inside the config/initializers/devise.rb file will do the trick.
In this way you don't need to override active_admin templates.

Related

How to change the name given with devise ?

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

Rails ActiveAdmin Routing new Resources

I'm just starting out with Rails and I decided to try out ActiveAdmin last night. I was able to register a new resource name 'Pages' in my ActiveAdmin app, but there's one thing I can't figure out how to customize with it.
I create a new Page with ActiveAdmin, but it's published within the admin/.. path.
(e.g. mydomain/admin/page/1)
How do I change the routing so the page can be viewed at mydomain/page/1?
Are you able to change the routing of existing resources in ActiveAdmin?
I'm very new at Rails so I assume this is a pretty easy fix. I plan to run through some more tutorials/books so I can better understand routing.
You can change the default admin namespace.
To do so you have to go to config/initializers/active_admin.rb file and find the following configuration:
# Default:
# config.default_namespace = :admin
Uncomment the line and set the default_namespace to whatever you need.
However, if you need to turn off the namespace at all, you will have to set the default_namespace to false:
config.default_namespace = false
This will allow you to run the AA from the root.
By doing so be aware of changes in routes:
if changed the namespace to hello, the admin_games_path becomes hello_games_path;
if changed to no namespace, use normal routes: admin_games_path becomes games_path.

Rails Generate Devise:Controllers not Working

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

Scoped views do not work

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/

How to create controllers and views to display a User's profile with Devise gem as authentication mechanism?

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

Resources