Rails App Set Up

By | 17/12/2019

My default prototyping for an app is still Rails, it’s so quick and easy – if I just want to build something to test out an idea I think the framework is amazing.

Here’s what I tend to do when first setting up, note I do the initial create using RubyMine which is as easy as selecting the options as below.

From the command line I’d type

$ rails new testProject -d postgresql

Allow Rails to run the bundle command and webpacker, and you should receive a nice ‘Process finished with exit code 0’ alert!

I tend to add some additional gems as standard:

group :development do  
    gem 'web-console'
    gem 'listen' 
    gem 'spring' 
    gem 'spring-watcher-listen'
    gem 'better_errors'
    gem 'rack-mini-profiler'  
    gem 'erb2haml'
    gem 'binding_of_caller'
    gem 'faker'
end
group :test do  
    gem 'selenium-webdriver'  
    gem 'webdrivers'  
    gem 'database_cleaner' :x64_mingw]  
    gem 'rspec-rails' 
    gem 'capybara'
end
##    Custom Gems   ##
gem 'haml', '~> 5.0', '>= 5.0.4'
gem 'devise', '~> 4.6', '>= 4.6.2'
gem 'simple_form', '~> 4.1'
gem 'dotenv-rails', :groups => [:development, :test]
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw], :groups => [:development, :test]

Don’t forget to run Bundle!

Also in Rails 6 you’ll need to set up Bootstrap with WebPacker as it’s no longer in a gem.

$ yarn add bootstrap jquery popper.js

Add the following to: config/webpack/environment.js

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
}))

module.exports = environment

Create a folder to hold stylesheets & add application stylesheet

$ mkdir app/javascript/packs/src/
$ touch app/javascript/packs/src/application.scss

Add the following to app/javascript/packs/application.js

// import 'bootstrap'
require("bootstrap/dist/js/bootstrap")
// import all stylesheets
import './src/application.scss'
// Add any other stylesheets here

Add the following to app/assets/stylesheets/application.css

@import "bootstrap/scss/bootstrap";

From the app/views/layouts/application.html.erb file remove thefollowing as we’re loading through webpack

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>

Head to config/database.yml and fill in local database details and database names

default: &default  
adapter: postgresql
encoding: unicode
username: database_username
password: database_password

pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development: <<: *default
database: test_db_dev

test: <<: *default
database: test_db_test
production: <<: *default

database: test_db_prod

password: <%= ENV['UNTITLED_DATABASE_PASSWORD'] %>

Then run

$ rails db:create && rails db:migrate

Final few things – install simple form & devise

$ rails generate simple_form:install --bootstrap
$ rails g devise:install 

I then normally set up some static pages to root to for home

$ rails g controller Pages home about faq news contact

Add the following to config/routes.rb

root to: "pages#home"

And finally let’s convert all the pages from erb to haml…

$ rails haml:replace_erbs

Boot up the server and test it’s all working

$ rails s