Upload
rory-gianni
View
581
Download
1
Embed Size (px)
Citation preview
● 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
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 'sassrails', '~> 3.1.4' gem 'coffeerails', '~> 3.1.1' gem 'uglifier', '>= 1.0.3'end
gem 'jqueryrails'
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 %>
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/