Skip to content

Instantly share code, notes, and snippets.

@stujo
Last active September 29, 2015 18:11
Show Gist options
  • Save stujo/147d46fe4c77feb0dee8 to your computer and use it in GitHub Desktop.
Save stujo/147d46fe4c77feb0dee8 to your computer and use it in GitHub Desktop.
Intro to Rails from Sinatra Notes - Stujo

Rails vs Sinatra

Creating a new Rails Application

$ rails new --help

Note To (Instructor/Self): Remove ~/.railsrc before Demo

$ cat ~/.railsrc
-d=postgresql
-T
-B
--skip-spring

Note: Overview of creating a new rails application, point out comparisions to sinatra as you go

$ rails new blog -d foo
  • databases - prefer postgresql
$ rails new blog -d postgresql -T

Walkthrough Generated Files

$ cd blog
$ tree -d

Starting a Rails Application

$ rails s

OOPS :) We need to do a little more

Initializing the Database

$ cat Rakefile
$ rake -T
$ rake db:create
$ rails s
$ open http://localhost:3000/
  • TADA! - (Hopefully)

Building Your Application

Pro Tip: Rails Console

$ rails c

Controllers

  • rails g controller articles index
  • But that generates some things we don't want....
  • rails g controller articles index --no-helper --no-assets
  • redirect_to instead of Sinatra's redirect

Views

  • ERB the same as we did in Sinatra.
  • What is Implicit Render?
  • View Helpers link_to, form_for

Routing

  • $ cat config/routes.rb
  • $ rake routes
  • _path and _url helpers used in views and redirects
$ rails c
> app.pizzas_url
 => "http://www.example.com/pizzas" 
> app.edit_pizza_url(1)
 => "http://www.example.com/pizzas/1/edit" 

Models

  • $ rails generate model Article title body:text
  • $ rake db:migrate
  • $ rails console (or rails c)
  • $ rails db - can fire up psql
  • > Article.create!(:title => 'Hello World', :body => 'The answer is 42')

Tie it All Together with resources

  • resources :articles
$ rails s
$ open http://localhost:3000/articles/index

And Go Do It!

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