Skip to content

Instantly share code, notes, and snippets.

@bih
Last active Apr 1, 2017
Embed
What would you like to do?
Intro to Rails 5 Workshop

Intro to Rails 5 Workshop

0. Configuration

Ruby 2.4.0 & Rails 5.2.0

How to install Ruby 2.4.0

$ curl -sSL https://get.rvm.io | bash -s stable --ruby=2.4.0

How to install Rails 5.2.0

$ gem install rails -v 5.2.0

1. Create Rails Project

$ mkdir ~/sites
$ cd ~/sites
$ rails new mlh-prime --skip-turbolinks --skip-action-cable --skip-action-mailer --javascript=jquery --database=sqlite3
$ cd mlh-prime

2. Create a Model-View-Controller

Run the following command:

$ rails generate scaffold [MODEL_NAME_SINGULAR] [*FIELD_NAME:TYPE]
$ rails generate scaffold Meetup name:string description:text location:string public:boolean time:datetime registration_url:string

Expected output:

invoke  active_record
create    db/migrate/20170331111551_create_meetups.rb
create    app/models/meetup.rb
invoke    test_unit
create      test/models/meetup_test.rb
create      test/fixtures/meetups.yml
invoke  resource_route
 route    resources :meetups
invoke  scaffold_controller
create    app/controllers/meetups_controller.rb
invoke    erb
create      app/views/meetups
create      app/views/meetups/index.html.erb
create      app/views/meetups/edit.html.erb
create      app/views/meetups/show.html.erb
create      app/views/meetups/new.html.erb
create      app/views/meetups/_form.html.erb
invoke    test_unit
create      test/controllers/meetups_controller_test.rb
invoke    helper
create      app/helpers/meetups_helper.rb
invoke      test_unit
invoke    jbuilder
create      app/views/meetups/index.json.jbuilder
create      app/views/meetups/show.json.jbuilder
create      app/views/meetups/_meetup.json.jbuilder
invoke  assets
invoke    coffee
create      app/assets/javascripts/meetups.coffee
invoke    scss
create      app/assets/stylesheets/meetups.scss
invoke  scss
create    app/assets/stylesheets/scaffolds.scss

Reference

3. Migrate Database

$ rails db:migrate

Expected output:

== 20170331112240 CreateMeetups: migrating ====================================
-- create_table(:meetups)
   -> 0.0007s
== 20170331112240 CreateMeetups: migrated (0.0007s) ===========================

4. Start server

$ rails server --daemon

(And if you ever want to kill the server, you can run $ kill $(cat tmp/pids/server.pid))

5. Add some validations to Meetup model

To recall, our fields are:

name:string
description:text
location:string
public:boolean
time:datetime
registration_url:string

Update app/models/meetup.rb:

# app/models/meetup.rb

class Meetup < ApplicationRecord
  validates :name, presence: true, length: { minimum: 4, maximum: 100 }, uniqueness: { scope: :location }
  validates :description, presence: true, length: { minimum: 20, maximum: 400 }
  validates :location, presence: true, inclusion: { in: %w(London Manchester Birmingham) }
  validates :time, presence: true
  validates :registration_url, presence: true
end

Reference

6. Update routes.rb

Update config/routes.rb:

# config/routes.rb

Rails.application.routes.draw do
  resources :meetups
  root 'meetups#index'
end

Reference

7. Create a Registrations Controller

Run the command:

$ rails generate controller meetups/registrations new

Update config/routes.rb:

# config/routes.rb

Rails.application.routes.draw do
  resources :meetups do
    resources :registrations, only: [:new]
  end

  root 'meetups#index'
end

In app/views/meetups/show.html.erb, on the line where it says:

<%= link_to 'Edit', edit_meetup_path(@meetup) %> |

Add before:

<%= link_to 'Register', new_meetup_registration_path(@meetup) %> |

TODO: Update registrations controller to a) set the meetup and b) redirect to the registration_url

8. Extra Tasks

  • Add URL validation for our registrations controller
  • Add some styling around each meetup
  • Add some JavaScript interactivity
  • Add a JSON endpoint
  • Deploy to Heroku
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment