RoR 101: Session 5


Published on

Part 5 of 6

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

RoR 101: Session 5

  1. 1. Building Web Apps with Rails V
  2. 2. Recap● Generating Controllers● Nesting Resources ● Nesting in Routes ● Nesting in Controllers ● Nesting in Views
  3. 3. First FMrails generate controller streamsindex new create destroyIn other words:rails generate controller <controller name> <actions>
  4. 4. 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
  5. 5. streams_controller.rbdef index @streams = Stream.allend We want to select a station and get all its streams
  6. 6. Answer: were going to pass it in the url by nesting resourceshttp:// Were going to tell the routes to interpret this as :station_id
  7. 7. Session 5: Authentication● User Model● Installing Gems with Bundler● Authentication with Devise
  8. 8. Session 5: Authentication At present, anyone can CRUD (Create Read Update Destroy)
  9. 9. Session 5: Authentication At present, anyone can CRUD (Create Read Update Destroy)Signed in Users Public
  10. 10. Session 5: Authentication At present, anyone can CRUD (Create Read Update Destroy)Signed in Users Public
  11. 11. Authentication: How?● Create a user model● Authenticate using email & password● Create a session● Were going to use Devise to do this
  12. 12. 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
  13. 13. Installing Devise with Install and manage gems Gems are specified in the gemfile This is found in the firstfm directory
  14. 14. gem rails, 3.1.1# Bundle edge Rails instead:# gem rails, :git => git:// sqlite3# Gems used only for assets and not required# in production environments by :assets do  gem sass­rails,   ~> 3.1.4  gem coffee­rails, ~> 3.1.1 We can specify:  gem uglifier, >= 1.0.3end Versions Sourcesgem jquery­rails When to use themgroup :test do  # Pretty printed test output  gem turn, :require => false  gem minitestend The Gemfile
  15. 15. Installing Devise Add the line gem devise To your gemfile, in the terminal run:bundle installrails generate devise:install
  16. 16. Generating the User Model We will use Devises optional template to generate a user model.rails generate devise User This gives us: User Model Migration for User Controller & Views for Users User Routes
  17. 17. Generated User Model & StuffCheck out routes with rake routesIn models/user.rb:devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable (A variety of modules provided by devise)
  18. 18. Adding the User Model Once youre happy, run the migration script:rake db:migrate … fire up the server and go to
  19. 19. More Devise GoodiesReturns the user object for the signed in user.current_userReturns true or false whether the user is signed in.user_signed_in?
  20. 20. Am I logged in?In views/layouts/Application.html.erb:<% if user_signed_in? %><p>hello <%= %></p><% end %>
  21. 21. So where are these views?rails generate devise:views
  22. 22. Task! Add Login LinksProvide links for our user to sign up, sign in / signout. (Hint! Check out the Devise wiki on Github)
  23. 23. Authenticate!Is the user authenticated?authenticate_user!
  24. 24. But be dry! Use a before filterbefore_filter :authenticate_user!
  25. 25. But be dry! Use a before filter before_filter :authenticate_user! e.g. class StationsController before_filter :authenticate_user!, :except => [:index, :show]…