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

Riding Rails

Riding Rails

Brief introduction to Ruby and Rails for a workshop that took place during CodeWeek @ DI in University of Minho

Fernando Mendes

October 16, 2014
Tweet

More Decks by Fernando Mendes

Other Decks in Programming

Transcript

  1. Riding Rails
    Code Week @ DI

    View Slide

  2. Miguel Pinto
    Fernando Mendes
    twitter: fribmendes
    github: frmendes
    github: miguelpinto98
    skype: miguelpinto.98

    View Slide

  3. View Slide

  4. gem install rails
    If you have Ruby installed:
    To install Ruby on OS X:
    1. Install Homebrew
    2. brew install rbenv ruby-build
    To install Ruby on Linux:
    \curl -sSL https://get.rvm.io | bash -s stable --ruby --rails

    View Slide

  5. What I will be talking about
    1. Ruby syntax
    2. Basic Rails
    What I won’t be talking about:
    1. Design
    2. Patterns
    3. Good practices
    Basic concepts = slides
    Everything else = examples

    View Slide

  6. Rails is Ruby

    View Slide

  7. Ruby is NOT Java
    Object oriented
    Testable
    Programmer happiness
    Everything is an object
    “So beautiful, tears are shed” _why, the lucky stiff

    View Slide

  8. Meet Matz
    Matz Not Matz

    View Slide

  9. Hello world!
    Ruby is all about idiomatic code
    We do not require parentheses

    View Slide

  10. Duck typing
    “If it walks like a duck, swims like a
    duck and quacks like a duck, it’s
    probably a duck.”

    View Slide

  11. Idiomatic ifs
    No curly brackets!
    No annoying ;
    One liners!
    Easy to read!

    View Slide

  12. Idiomatic ifs
    btw, this is our string
    interpolation

    View Slide

  13. Idiomatic ifs
    You can put this in the one-
    liner syntax

    View Slide

  14. Methods

    View Slide

  15. Methods
    Wait a second, where are we
    returning the values!?

    View Slide

  16. Methods
    Returns the last interpreted expression
    Ruby is smart
    Default values

    View Slide

  17. Classes
    Creates new instance with
    Elevator.new(weight, capacity)
    Translates
    to

    View Slide

  18. Classes
    Operator override just feels
    more natural.
    If you don’t like it, we also
    have Object#eql? and
    Object#equal?

    View Slide

  19. Classes
    Also, surprise surprise!
    This code has a trap. Can
    you find it?

    View Slide

  20. Classes
    Remember that the last
    evaluated expression is the
    one that gets returned?

    View Slide

  21. Classes
    Only this is going to get
    returned.
    If we wanted to write the correct method
    we would have to use the && operator.

    View Slide

  22. Classes
    public: default value, accessed by
    anyone
    protected: method can be accessed by
    the class or subclasses
    private: method can only be accessed by
    the class

    View Slide

  23. Classes
    class variable
    class method

    View Slide

  24. Blocks and High Order Functions
    Multi-line syntax
    One-line syntax

    View Slide

  25. Blocks and High Order Functions
    x is unchanged

    View Slide

  26. Blocks and High Order Functions
    x is unchanged
    x is changed
    Ruby convention:
    ! ending methods change the object
    ? ending methods return a boolean

    View Slide

  27. Symbols
    Strings assign
    different objects

    View Slide

  28. Symbols
    Symbols are always
    the same

    View Slide

  29. Wat the wtf is MVC

    View Slide

  30. Short answer:

    View Slide

  31. Not so short answer:

    View Slide

  32. My first app

    View Slide

  33. rails new project

    View Slide

  34. /app
    ..../controller
    ..../helpers
    ..../models
    ..../views
    ....../layouts
    /bin
    /config
    /db
    /doc
    /lib
    /log
    /public
    /test
    /tmp
    /vendor

    View Slide

  35. External libraries (gems) are imported into your project.
    Can have specified versions, minimum version, etc.
    Can Should be divided into groups
    Not necessarily Rails-exclusive
    Gemfile

    View Slide

  36. Gemfile
    bundle install

    View Slide

  37. Bringing up the server
    rails server
    Check it for yourself: go to localhost:3000

    View Slide

  38. Checking the console
    rails console

    View Slide

  39. rails generate scaffold User username:string name:string

    View Slide

  40. Scaffolding
    rails generate scaffold User username:string name:string
    model name

    View Slide

  41. Scaffolding
    rails generate scaffold User username:string name:string
    attribute name

    View Slide

  42. Scaffolding
    rails generate scaffold User username:string name:string
    attribute type

    View Slide

  43. Views
    Under app/views//

    View Slide

  44. Views
    Under app/views//
    By default, Rails uses embedded Ruby .html.erb

    View Slide

  45. Views
    Under app/views//
    By default, Rails uses embedded Ruby .html.erb
    Have access to the instance variables created by the controller.

    View Slide

  46. Into the CRUD
    Create
    Read
    Update
    Delete

    View Slide

  47. Models
    User.new(username: “frmendes”, name: “Fernando Mendes”)

    View Slide

  48. User.all User.count
    Models

    View Slide

  49. User.new(username: “frmendes”, name: “Fernando Mendes”).save
    Models

    View Slide

  50. User.create(username: “codeweek”, name: “at DI”)
    Models

    View Slide

  51. User.find(1) User.find_by(username: “frmendes”)
    Models

    View Slide

  52. Controllers and Routes

    View Slide

  53. Migrations

    View Slide

  54. Migrations
    rails generate migration AddIndexToUsers

    View Slide

  55. Validations

    View Slide

  56. Ruby Make
    rake db:{drop,migrate}
    You better be
    f*cking careful!!1!

    View Slide

  57. Relationships

    View Slide

  58. Miguel Pinto
    Fernando Mendes
    twitter: fribmendes
    github: frmendes
    github: miguelpinto98
    skype: miguelpinto.98

    View Slide