26
Building Web Apps with Rails V

RoR 101: Session 5

Embed Size (px)

Citation preview

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 <controller name> <actions>

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

def index@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'

# 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

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 rake routes

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:

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

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. 

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