Skip to content

Instantly share code, notes, and snippets.

@icyflame
Last active August 29, 2015 14:07
Show Gist options
  • Save icyflame/29c4a04515e8f9063bb7 to your computer and use it in GitHub Desktop.
Save icyflame/29c4a04515e8f9063bb7 to your computer and use it in GitHub Desktop.
The set of commands that are required to get started with a rails application

Application Setup: Install Devise, Bootstrap, ActiveAdmin

  • 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 file application.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

Creating the initializers for Devise, ActiveAdmin

  • 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 (devise) models

  • 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

Creating controllers

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.

Other Useful Stuff

  • 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

Resources

@import "bootstrap-sprockets";
@import "bootstrap";
html, body{
margin-left: 20px;
margin-right: 20px;
margin-top: 20px;
}
form input, textarea{
@extend .form-control;
margin-top: 10px;
margin-bottom: 10px;
}

Production

  • Rails Asset Pipeline
# config/environments/production.rb
config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true

Refer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment