RoR 101: Session 5

  • View
    569

  • Download
    1

Embed Size (px)

Text of RoR 101: Session 5

  • Building Web Apps with Rails

    V

  • Generating Controllers

    Nesting Resources

    Nesting in Routes Nesting in Controllers Nesting in Views

    Recap

  • 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

    defindex@streams=Stream.all

    end

    We want to select a station and get all its streams

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

    http://127.0.0.1:3000/stations/1/streams

    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'

    #BundleedgeRailsinstead:#gem'rails',:git=>'git://github.com/rails/rails.git'

    gem'sqlite3'

    #Gemsusedonlyforassetsandnotrequired#inproductionenvironmentsbydefault.group:assetsdogem'sassrails','~>3.1.4'gem'coffeerails','~>3.1.1'gem'uglifier','>=1.0.3'end

    gem'jqueryrails'

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

    We can specify:VersionsSources

    When to use them

  • Installing Devise

    Add the line

    gem'devise'

    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:

    devise:database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable

    (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

    127.0.0.1:3000/users/sign_up

    rake db:migrate

  • More Devise Goodies

    Returns the user object for the signed in user.

    current_user

    Returns true or false whether the user is signed in.

    user_signed_in?

  • Am I logged in?

    In views/layouts/Application.html.erb:

    hello

  • 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)

    https://github.com/plataformatec/devise/wiki/

  • Authenticate!

    Is the user authenticated?

    authenticate_user!

  • But be dry!

    Use a before filter

    before_filter:authenticate_user!

  • But be dry!

    Use a before filter

    before_filter:authenticate_user!

    e.g.

    classStationsControllerbefore_filter:authenticate_user!,:except=>[:index,:show]

    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