Top Banner
Building Web Apps with Rails V
26

RoR 101: Session 5

Jul 13, 2015

Download

Technology

Rory Gianni
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RoR 101: Session 5

Building Web Apps with Rails

V

Page 2: RoR 101: Session 5

● Generating Controllers

● Nesting Resources

● Nesting in Routes● Nesting in Controllers● Nesting in Views

Recap

Page 3: RoR 101: Session 5

First FM

rails generate controller streamsindex new create destroy

In other words:

rails generate controller <controller name> <actions>

Page 4: RoR 101: Session 5

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

Page 5: RoR 101: Session 5

streams_controller.rb

def index@streams = Stream.all

end

We want to select a station and get all its streams

Page 6: RoR 101: Session 5

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

Page 7: RoR 101: Session 5

Session 5: Authentication

● User Model● Installing Gems with Bundler● Authentication with Devise

Page 8: RoR 101: Session 5

Session 5: Authentication

At present, anyone can CRUD

(Create Read Update Destroy)

Page 9: RoR 101: Session 5

Session 5: Authentication

At present, anyone can CRUD

(Create Read Update Destroy)

Signed in Users Public

Page 10: RoR 101: Session 5

Session 5: Authentication

At present, anyone can CRUD

(Create Read Update Destroy)

Signed in Users Public

Page 11: RoR 101: Session 5

Authentication: How?

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

Page 12: RoR 101: Session 5

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

Page 13: RoR 101: Session 5
Page 14: RoR 101: Session 5

Installing Devise with

Install and manage gems

Gems are specified in the gemfile

This is found in the 'firstfm' directory

Page 15: RoR 101: Session 5

gem 'rails', '3.1.1'

# Bundle edge Rails instead:# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

# Gems used only for assets and not required# in production environments by default.group :assets do  gem 'sass­rails',   '~> 3.1.4'  gem 'coffee­rails', '~> 3.1.1'  gem 'uglifier', '>= 1.0.3'end

gem 'jquery­rails'

group :test do  # Pretty printed test output  gem 'turn', :require => false  gem 'minitest'end The Gemfile

We can specify:VersionsSources

When to use them

Page 16: RoR 101: Session 5

Installing Devise

Add the line

gem 'devise'

To your gemfile, in the terminal run:

bundle install

rails generate devise:install

Page 17: RoR 101: Session 5

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

Page 18: RoR 101: Session 5

Generated User Model & Stuff

Check out routes with rake routes

In models/user.rb:

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

(A variety of modules provided by devise)

Page 19: RoR 101: Session 5

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

Page 20: RoR 101: Session 5

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?

Page 21: RoR 101: Session 5

Am I logged in?

In views/layouts/Application.html.erb:

<% if user_signed_in? %><p>hello <%= current_user.email %></p><% end %>

Page 22: RoR 101: Session 5

So where are these views?

rails generate devise:views

Page 23: RoR 101: Session 5

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/

Page 24: RoR 101: Session 5

Authenticate!

Is the user authenticated?

authenticate_user!

Page 25: RoR 101: Session 5

But be dry!

Use a before filter

before_filter :authenticate_user!

Page 26: RoR 101: Session 5

But be dry!

Use a before filter

before_filter :authenticate_user!

e.g. 

class StationsController before_filter :authenticate_user!, :except => [:index, :show]…