2. Recap
● Generating Controllers
● Nesting Resources
● Nesting in Routes
● Nesting in Controllers
● Nesting in Views
3. First FM
rails generate controller streams
index new create destroy
In other words:
rails generate controller <controller name> <actions>
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
6. 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 interpret
this as :station_id
9. Session 5: Authentication
At present, anyone can CRUD
(Create Read Update Destroy)
Signed in Users Public
10. Session 5: Authentication
At present, anyone can CRUD
(Create Read Update Destroy)
Signed in Users Public
11. Authentication: How?
● Create a user model
● Authenticate using email & password
● Create a session
● We're going to use Devise to do this
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.
14. Installing Devise with
Install and manage gems
Gems are specified in the gemfile
This is found in the 'firstfm' directory
16. Installing Devise
Add the line
gem 'devise'
To your gemfile, in the terminal run:
bundle install
rails generate devise:install
17. Generating the User Model
We will use Devise's 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
18. 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)
19. Adding the User Model
Once you're happy, run the migration script:
rake db:migrate
… fire up the server and go to
127.0.0.1:3000/users/sign_up
20. 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?
21. Am I logged in?
In views/layouts/Application.html.erb:
<% if user_signed_in? %>
<p>hello <%= current_user.email %></p>
<% end %>
22. So where are these views?
rails generate devise:views
23. 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/
25. But be dry!
Use a before filter
before_filter :authenticate_user!
26. But be dry!
Use a before filter
before_filter :authenticate_user!
e.g.
class StationsController
before_filter
:authenticate_user!, :except =>
[:index, :show]
…