Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby on Rails Workshop

Ruby on Rails Workshop

Part of an introductory Ruby and Rails curriculum for more than 100 freshman students for a summer application development course (NUS Orbital) at the National University of Singapore.

Yos Riady

May 12, 2015
Tweet

More Decks by Yos Riady

Other Decks in Programming

Transcript

  1. Ruby on Rails
    Yos Riady
    yosriady.com

    View Slide

  2. View Slide

  3. goo.gl/waonpj

    View Slide

  4. View Slide

  5. Today’s Agenda
    ● Intro to Ruby (40 minutes)
    ● Rails (120 minutes)
    ● QA (20 minutes)

    View Slide

  6. Introduction
    rubyonrails.org
    ruby-lang.org

    View Slide

  7. View Slide

  8. Built On Rails

    View Slide

  9. View Slide

  10. Ruby Language
    codecademy.com/en/tracks/ruby
    ● Variables
    ● Conditionals
    ● Loops + Iterators
    ● Blocks
    ● Data Structures
    ● Classes

    View Slide

  11. Installing Rails
    https://lite.nitrous.io

    View Slide

  12. Creating a New Rails Application
    rails new myapp
    cd myapp
    bundle install
    rails server

    View Slide

  13. MVC Architecture

    View Slide

  14. Building a Static Site
    rails generate controller Pages home
    Now try and visit ‘/pages/home’.
    What do you see?

    View Slide

  15. View Slide

  16. Building a Static Site
    Try adding a few more pages on your own.
    Remember, for each page:
    ● Add a new route in routes.rb
    ● Add a new action in PagesController.rb
    ● Add a view .erb file

    View Slide

  17. Building a Static Site
    Try adding the following in your action:
    def home
    @now = Time.now
    end
    In your home.html.erb file, add:
    <%= @now %>

    View Slide

  18. Building a Dynamic Site
    At the core, most database driven web sites are the same. They need to store
    records and provide a way to do the following:
    ● Create new records in the database
    ● Read or show the records in the database
    ● Update existing records
    ● Destroy or delete records
    Because these 4 actions are so common, Rails includes the scaffold
    command to make creating them easier.
    rails generate scaffold Confession title:string body:text

    View Slide

  19. View Slide

  20. View Slide

  21. REST

    View Slide

  22. Let’s add comments to our application!
    Users can create Comments for a Confession,
    and have it visible under the Confession entry.
    How do we do this?
    Building a Dynamic Site

    View Slide

  23. What do we need?
    ● A new Comment model
    ● A new controller for Comments
    ○ A ‘create’ action to save new Comments to the DB
    ● A new route in routes.rb for
    ‘comments#create’
    ● A view form for users to write a Comment
    Building a Dynamic Site

    View Slide

  24. Building a Dynamic Site

    View Slide

  25. Hands-On

    View Slide

  26. Learning Resources
    ● http://tryruby.org/
    ● http://guides.rubyonrails.org/
    ● http://docs.railsbridge.org/intro-to-rails/
    ● http://rails-4-0.railstutorial.org
    ● http://www.codecademy.com/learn/learn-rails

    View Slide

  27. View Slide

  28. Q&A
    Yos Riady
    yosriady.com

    View Slide

  29. View Slide