RoR 101: Session 5

  • View

  • Download

Embed Size (px)

Text of RoR 101: Session 5

  • Building Web Apps with Rails


  • Generating Controllers

    Nesting Resources

    Nesting in Routes Nesting in Controllers Nesting in Views


  • First FM

    rails generate controller streamsindex new create destroy

    In other words:

    rails generate controller

  • StreamsController should really...

    Index - show all streams belonging to a station

    New - show the form to create a stream belonging to a station

    Create - take form data and saves a stream belonging to a station

    Destroy - delete a stream from the database

  • streams_controller.rb



    We want to select a station and get all its streams

  • Answer: we're going to pass it in the url by nesting resources

    We're going to tell the routes to interpretthis as :station_id

  • Session 5: Authentication

    User Model Installing Gems with Bundler Authentication with Devise

  • Session 5: Authentication

    At present, anyone can CRUD

    (Create Read Update Destroy)

  • Session 5: Authentication

    At present, anyone can CRUD

    (Create Read Update Destroy)

    Signed in Users Public

  • Session 5: Authentication

    At present, anyone can CRUD

    (Create Read Update Destroy)

    Signed in Users Public

  • Authentication: How?

    Create a user model Authenticate using email & password Create a session We're going to use Devise to do this

  • Authentication: How?

    Devise Provides:

    MVC components for authentication A controller for creating / destroying sessions A sign in form Links / Routes to sign in / sign out

  • Installing Devise with

    Install and manage gems

    Gems are specified in the gemfile

    This is found in the 'firstfm' directory

  • gem'rails','3.1.1'




    group:testdo#Prettyprintedtestoutputgem'turn',:require=>falsegem'minitest'end The Gemfile

    We can specify:VersionsSources

    When to use them

  • Installing Devise

    Add the line


    To your gemfile, in the terminal run:

    bundle install

    rails generate devise:install

  • Generating the User Model

    We will use Devise's optional template to generate a user model.

    This gives us:

    User ModelMigration for User

    Controller & Views for UsersUser Routes

    rails generate devise User

  • Generated User Model & Stuff

    Check out routes with rakeroutes

    In models/user.rb:


    (A variety of modules provided by devise)

  • Adding the User Model

    Once you're happy, run the migration script:

    fire up the server and go to

    rake db:migrate

  • More Devise Goodies

    Returns the user object for the signed in user.


    Returns true or false whether the user is signed in.


  • Am I logged in?

    In views/layouts/Application.html.erb:


  • So where are these views?

    rails generate devise:views

  • Task! Add Login Links

    Provide links for our user to sign up, sign in / sign out.

    (Hint! Check out the Devise wiki on Github)

  • Authenticate!

    Is the user authenticated?


  • But be dry!

    Use a before filter


  • But be dry!

    Use a before filter




    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26