- It's a class --- and requests to the controller are just instances of the class
- Controllers are responsible for doing one of two things
- rendering something or redirecting to another URL
- Sinatra v. Rails
- The bulk of what you'd put in sinatra routes, are written in methods within the controller
- index
- new
- create
- update
- delete
- show
- edit
- rake routes v. sinatra index.rb
- break out your routes into controllers
- route resources :users
- possibly show a complete rails app
Before filters run on requests before the request get to the controller's actions.
So, authenticate before granting access to do something — the perfect before filter opportunity!
After filter runs after, duh. They can modify responses, like if you need to send an email after a user has taken an action.
- before_filters
- after_filters
- around_filters
LEARNING OPPORTUNITY: Shadi, want to explain around filters!?!
- pretty much the same as sinatra
- Rails apps have a session for each user:
- Only available in the view and the controller
- Sessions ID's are always stored in the cookie, you should never pass sessions thru a URL
- ActionDispatch::Session::CookieStore – Stores everything on the client.
- ActiveRecord::SessionStore – Stores the data in a database using Active Record.
- ActionDispatch::Session::CacheStore – Stores the data in the Rails cache.
- ActionDispatch::Session::MemCacheStore – Stores the data in a memcached cluster (this is a legacy implementation; consider using CacheStore instead).
You can configure where the session is stored with some code by creating a session_store.rb file
YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session'
-
Just like a hash:
-
i.e. if you wanna get anything or store anything in a session all you gotta do is
session[:foobar] = "Neil"
session[:foobar] == "Neil"?
- like sessions, cookies work like hashes, you can store anything in them.
cookies[:foobar] = "foo"
cookies[:foobar] == "foo"?
- Can be defined within the controller using the helper_method :method
- Show examples
- CSRF and protect_from_forgery