Ruby on Rails
Workshop
Created by
Andreas Grohmann
/
andreas.grohmann@gmx.de
/
twitter.com/grohmeo
# Ruby on Rails * [motivation](#/2) * [overview](#/3) * [getting started](#/4)
## motivation * need small application for microservice * start from scratch * as lean as possible * result driven * well supported * exchangeable
## build in rails * [Airbnb](https://www.airbnb.de/) * [GitHub](https://github.com/) * [Shopify](https://www.shopify.com/) * [Twitch](http://twitch.tv/) * [Kickstarter](http://www.kickstarter.com/)
## overview ### facts * open source webframework * based on ruby * current version: 2.3.4 * presented: 07.2004
### principles * dont repeat yourself - DRY * each information exists one-time * e. g. auto generated get/set methods for db by ActiveRecord * low risk of inconsistencies * convention over configuration * e.g. rules for pk's, name: id, type: bigint * e.g. relation between model: Customer file: /app/models/customer.rb table: customers * e.g. rules for fk's fk_column: customer_id
## getting started ### build an app generate Dockerfile ``` # ./Dockerfile FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp ```
generate Gemfile (take current versions) ``` # ./Gemfile source 'https://rubygems.org' gem 'rails', '5.1.1' ``` generate empty Gemfile.lock ``` $ touch Gemfile.lock ```
generate docker-compose.yml ``` # ./docker-compose.yml version: '2' services: db: image: postgres web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db ```
check ``` $ ls Dockerfile Gemfile Gemfile.lock docker-compose.yml ```
build a new rails app ``` $ docker-compose run --rm web rails new . --force --database=postgresql --skip-bundle ```
check generated app files ``` $ ls Dockerfile Rakefile config.ru log tmp Gemfile app db package.json vendor Gemfile.lock bin docker-compose.yml public README.md config lib test ```
after editing the Gemfile ``` $ docker-compose build ```
connect rails app to database ``` # ./config/database.yml development: &default adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: postgres password: host: db test: <<: *default database: myapp_test ```
create the database ``` $ docker-compose run --rm web rake db:create ``` boot the rails app ``` $ docker-compose up -d ```
check the rails app
http://localhost:3000
### add something to db stop the containers ``` $ docker-compose stop ``` add model, view, controler, tests ``` $ docker-compose run --rm web rails generate scaffold HighScore game:string score:integer ``` build containers again ``` $ docker-compose build ``` start containers ``` $ docker-compose up -d ```
What went wrong?
http://localhost:3000
### migrate the changes in db model ``` $ docker-compose stop $ docker-compose run --rm web rake db:migrate ``` output ``` Starting workshoptest3_db_1 == 20170518155127 CreateHighScores: migrating ================================= -- create_table(:high_scores) -> 0.0079s == 20170518155127 CreateHighScores: migrated (0.0080s) ======================== ``` start container again ``` $ docker-compose up -d ```
check the rails app
http://localhost:3000/high_scores
play around
http://localhost:3000/high_scores/new
http://localhost:3000/high_scores
got a REST API from scratch
http://localhost:3000/high_scores.json
### build an administration UI stop the containers ``` $ docker-compose stop ``` add gem to gemfile ``` # ./Gemfile ... gem "rails_admin", "~> 1.1.1" gem "erubis" ``` build the container again ``` $ docker-compose build ```
install gem ``` $ docker-compose run --rm web rails g rails_admin:install ``` output ``` ? Where do you want to mount rails_admin? Press
for [admin] > route mount RailsAdmin::Engine => '/admin', as: 'rails_admin' create config/initializers/rails_admin.rb ``` build the container again ``` $ docker-compose build $ docker-compose up -d ```
check the admin UI
http://localhost:3000/admin
check the admin UI
http://localhost:3000/admin/high_score
check other awesome gems
rubygems.org/
tutorial
www.railstutorial.org/book/beginning
### API Tutorials * [Using Rails for API-only Applications](http://edgeguides.rubyonrails.org/api_app.html) * [Build a RESTful JSON API With Rails 5](https://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-one) * [Building a JSON API with Rails 5](https://blog.codeship.com/building-a-json-api-with-rails-5/)
Questions?