New Rails Application - HVboom/HowTo-DigitalOcean GitHub Wiki

Create a new Ruby On Rails application

Environment setup

Basic setup

  • Check newest Ruby version: rvm list known
  • Find latest Rails branch: https://github.com/rails/rails/branches
  • Create new application: create_rails_project <project> [<ruby version | 2.4> <rails branch | 5-1-stable>] - e.g. create_rails_project BuildingIntegrationModeling
  • Define development target url: cd $HOME/RubyOnRails && ln -s <project> <url (all lower case - no special characters)> - e.g. ln -s BuildingIntegrationModeling bim
    • results in an application url: http://<user>.<url>.ror.hvboom.org - e.g. http://mario.bim.ror.hvboom.org

Update Gemfile

...

# Use jquery as the JavaScript library
gem 'jquery-rails'

# HAML is my preferred template language
gem 'haml-rails'

# I like Bootstrap
gem 'simple_form'
gem 'bootstrap-sass'

...

group :development, :test do
  ...

  # Testing with RSpec
  gem 'rspec-rails'
  gem 'factory_girl_rails'
  gem 'faker'

  # Better console
  gem 'pry-rails'
  gem 'pry-rescue'
end

group :development do
  ...

  # Used to generate a standard layout for Bootstrap frontend
  gem 'rails_layout'

  # I always make errors :-)
  gem 'better_errors'
  gem 'binding_of_caller'
end

group :test do
  # Everything you need for your Integration tests
  gem 'cucumber-rails', require: false
  gem 'capybara'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'selenium-webdriver'
end

...
  • Install all Gems using Bundler which will automatically run, when you go into you application directory cd $HOME/RubyOnRails/<project>
    • otherwise invoke bundler manually: bundle install

Setup MySQL

  • Rails configuration

    • Define config/database.yml:

      # MySQL. Versions 5.1.10 and up are supported.
      #
      # Install the MySQL driver
      #   gem install mysql2
      #
      # Ensure the MySQL gem is defined in your Gemfile
      #   gem 'mysql2'
      #
      # And be sure to use new-style password hashing:
      #   http://dev.mysql.com/doc/refman/5.7/en/old-client.html
      #
      default: &default
        adapter: mysql2
        encoding: utf8mb4
        pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
        socket: /tmp/mysql.sock
        database: <%= Rails.application.credentials[:mysql_db] %>
        username: <%= Rails.application.credentials[:mysql_username] %>
        password: <%= Rails.application.credentials[:mysql_password] %>
      
      development:
        <<: *default
      
      # Warning: The database defined as "test" will be erased and
      # re-generated from your development database when you run "rake".
      # Do not set this db to the same as development or production.
      test:
        <<: *default
      
      # As with config/secrets.yml, you never want to store sensitive information,
      # like your database password, in your source code. If your source code is
      # ever seen by anyone, they now have access to your database.
      #
      # Instead, provide the password as a unix environment variable when you boot
      # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
      # for a full rundown on how to provide these environment variables in a
      # production deployment.
      #
      # On Heroku and other platform providers, you may have a full connection URL
      # available as an environment variable. For example:
      #
      #   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
      #
      # You can use this database configuration with:
      #
      #   production:
      #     url: <%= ENV['DATABASE_URL'] %>
      #
      production:
        <<: *default
  • Create Secrets

    • Development example: config/secrets.yml.development.example

      # Be sure to restart your server when you modify this file.
      
      # Your secret key is used for verifying the integrity of signed cookies.
      # If you change this key, all old signed cookies will become invalid!
      
      # Make sure the secret is at least 30 characters and all random,
      # no regular words or you'll be exposed to dictionary attacks.
      # You can use `rails secret` to generate a secure secret key.
      
      # Make sure the secrets in this file are kept private
      # if you're sharing your code publicly
      db_connection: &db_connection
        mysql_username: <app>
        mysql_password: <db password>
      
      development:
        secret_key_base: <rails secret>
        # DB connection
        mysql_db: <app>_development
        <<: *db_connection
      
        # Better errors
        trusted_ip: <list of ip's>
      
      test:
        secret_key_base: <rails secret>
        # DB connection
        mysql_db: <app>_test
        <<: *db_connection
    • Production example: config/secrets.yml.production.example

      # Be sure to restart your server when you modify this file.
      
      # Your secret key is used for verifying the integrity of signed cookies.
      # If you change this key, all old signed cookies will become invalid!
      
      # Make sure the secret is at least 30 characters and all random,
      # no regular words or you'll be exposed to dictionary attacks.
      # You can use `rails secret` to generate a secure secret key.
      
      # Make sure the secrets in this file are kept private
      # if you're sharing your code publicly.
      
      # Do not keep production secrets in the repository,
      # instead read values from the environment.
      production:
        secret_key_base: <rails secret>
        # DB connection
        mysql_db: <app>_production
        mysql_username: <app>
        mysql_password: <MySQL password>
  • ❗Update .gitignore

    ...
    # Ignore sensitive files
    /config/secrets.yml
    ...
  • Create DB user ** via console

    mysql -u root -p
    create user '<app>'@'localhost' identified by '<mysql_password>';
    grant all privileges on `<app>\_%`.* TO '<app>'@'localhost';
    quit

** via phpMyAdmin

[[images/phpMyAdmin_AddApplicationUser.png]]
  • Initialize development and test DB: rails db:create:all

Setup basic application

  • Frontend framework

    rails generate layout:install bootstrap4
    rails generate layout:navigation bootstrap4
    rails generate simple_form:install --bootstrap
  • Testing

    rails generate rspec:install
    # Optional, if you do not want to user RSpec for integration tests too
    rails generate cucumber:install

Git repository

  • Local git repository

    git add -A
    git commit -m"Basic setup"
  • Remote git repository

    • Create new GitLab repository
    • Add local repository
      git remote add origin [email protected]:HVboom/<project>.git
      git push -u origin master
⚠️ **GitHub.com Fallback** ⚠️