- Create the application
rails new app
- Move into the app directory
cd app/
- Open the gemfile and add bootstrap and devise. (Assuming these two will be required in any real application)
gem 'bootstrap-sass'
gem 'devise'
- Run the bundler
bundle install
- Include bootstrap in the application's stylesheets. (
application.css.scss
, delete the fileapplication.css
)
@import 'bootstrap-sprockets';
@import 'bootstrap';
- Add the active admin gem.
Behind a proxy:
gem 'activeadmin', :git => 'https://github.com/activeadmin/activeadmin.git'
OR
Using SSH
gem 'activeadmin', github: 'activeadmin'
OR
Using SSH
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'polyamorous', github: 'activerecord-hackery/polyamorous'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'formtastic', github: 'justinfrench/formtastic'
- Run the bundler
bundle install
- Create the devise initializer
rails g devise:install
- Create the active admin initializer
rails g active_admin:install
- Add the notice and alert paragraphs required by devise to the
application.html.erb
layout
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
- Create a rails model
rails g model Student name:string rollnum:string
Add all the user defined fields here, like Name, Phone Number etc. Don't add email and password, as these will be added by devise, and adding it here will throw a conflict, when adding devise to this model.
- Add devise to this model
rails g devise Student
If the names are the same, then devise will be added to the model. If the names are not the same, then the model will be created with only the email, password and other devise related fields.
- Register this model with ActiveAdmin
rails g active_admin:resource Student
- Login to the ActiveAdmin portal
Default login:
email: admin@example.com
password: password
rails g articles index show update delete
Adding actions here is advisable because the views for the action and the skeleton of the actions are created by rails, and this reduces the file creation time a lot.
- Permitting other parameters in a Devise Sign up or Account update
Add the following code in your Application Controller [application_controller.rb
] in the ApplicationController
class.
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
devise_parameter_sanitizer.for(:sign_up) << :dateofbirth
devise_parameter_sanitizer.for(:account_update) << :name
devise_parameter_sanitizer.for(:account_update) << :dateofbirth
end
Trick: You can restrict the user from changing username using the above permitted parameters approach.\
- CRUD from ActiveAdmin
Add the following line to the model.rb
file present inside app/admin
.
permit_params :email, :name
Note: Without these lines, CRUD from ActiveAdmin portal is not possible.
- Changing the Devise Views for Registrations, Sessions etc
rails generate devise:views students -v registration sessions
Set in the config/initializers/devise.rb
file:
config.scoped_views = true