aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/actioncontroller/session.txt
diff options
context:
space:
mode:
Diffstat (limited to 'railties/doc/guides/actioncontroller/session.txt')
-rw-r--r--railties/doc/guides/actioncontroller/session.txt93
1 files changed, 74 insertions, 19 deletions
diff --git a/railties/doc/guides/actioncontroller/session.txt b/railties/doc/guides/actioncontroller/session.txt
index 50ed76e3ae..467cffbf85 100644
--- a/railties/doc/guides/actioncontroller/session.txt
+++ b/railties/doc/guides/actioncontroller/session.txt
@@ -1,30 +1,62 @@
== Session ==
-Your application sets up a session for each user which can persist small amounts of data between requests. The session is only available in the controller. It can be stored in a number of different session stores:
-
-TODO: Not sure if all of these are available by default.
+Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and can use one of a number of different storage mechanisms:
* CookieStore - Stores everything on the client.
- * SQLSessionStore - Stores the data in a database using SQL.
* DRBStore - Stores the data on a DRb client.
* MemCacheStore - Stores the data in MemCache.
* ActiveRecordStore - Stores the data in a database using Active Record.
All session stores store the session id in a cookie - there is no other way of passing it to the server. Most stores also use this key to locate the session data on the server.
-The default and recommended store, the Cookie Store, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents. It can only store 4Kb of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. Expecially discouraged is storing complex objects (anything other than basic Ruby objects) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The Cookie Store has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.
+The default and recommended store, the Cookie Store, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents. It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. Expecially discouraged is storing complex objects (anything other than basic Ruby objects, the primary example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The Cookie Store has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.
If you need a different session storage mechanism, you can change it in the `config/environment.rb` file:
[source, ruby]
------------------------------------------
-# Set to one of [:active_record_store, :sql_session_store, :drb_store, :mem_cache_store, :cookie_store]
+# Set to one of [:active_record_store, :drb_store, :mem_cache_store, :cookie_store]
config.action_controller.session_store = :active_record_store
------------------------------------------
+=== Disabling the session ===
+
+Sometimes you don't need a session, and you can turn it off to avoid the unnecessary overhead. To do this, use the link:http://api.rubyonrails.org/classes/ActionController/SessionManagement/ClassMethods.html#M000649[session] class method in your controller:
+
+[source, ruby]
+------------------------------------------
+class ApplicationController < ActionController::Base
+ session :off
+end
+------------------------------------------
+
+You can also turn the session on or off for a single controller:
+
+[source, ruby]
+------------------------------------------
+# The session is turned off by default in ApplicationController, but we
+# want to turn it on for log in/out.
+class LoginsController < ActionController::Base
+ session :on
+end
+------------------------------------------
+
+Or even a single action:
+
+[source, ruby]
+------------------------------------------
+class ProductsController < ActionController::Base
+ session :on, :only => [:create, :update]
+end
+------------------------------------------
+
=== Accessing the session ===
-In your controller you can access the session through the `session` method. Session values are stored using key/value pairs like a hash:
+In your controller you can access the session through the `session` instance method.
+
+NOTE: There are two `session` methods, the class and the instance method. The class method which is described above is used to turn the session on and off while the instance method described below is used to access session values. The class method is used outside of method definitions while the instance methods is used inside methods, in actions or filters.
+
+Session values are stored using key/value pairs like a hash:
[source, ruby]
------------------------------------------
@@ -33,6 +65,8 @@ class ApplicationController < ActionController::Base
private
# Finds the User with the ID stored in the session with the key :current_user_id
+ # This is a common way to do user login in a Rails application; logging in sets the
+ # session value and logging out removes it.
def current_user
@_current_user ||= session[:current_user_id] && User.find(session[:current_user_id])
end
@@ -74,7 +108,7 @@ class LoginsController < ApplicationController
end
------------------------------------------
-To reset the entire session, use `reset_session`.
+To reset the entire session, use link:http://api.rubyonrails.org/classes/ActionController/Base.html#M000855[reset_session].
=== The flash ===
@@ -95,18 +129,18 @@ end
The `destroy` action redirects to the application's `root_url`, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to a display eventual errors or notices from the flash in the application's layout:
-[source, ruby]
------------------------------------------
-<!-- head, etc -->
-<body>
- <% if flash[:notice] -%>
- <p class="notice"><%= flash[:notice] %></p>
- <% end -%>
- <% if flash[:error] -%>
- <p class="error"><%= flash[:error] %></p>
- <% end -%>
- <!-- more content -->
-</body>
+<html>
+ <!-- <head/> -->
+ <body>
+ <% if flash[:notice] -%>
+ <p class="notice"><%= flash[:notice] %></p>
+ <% end -%>
+ <% if flash[:error] -%>
+ <p class="error"><%= flash[:error] %></p>
+ <% end -%>
+ <!-- more content -->
+ </body>
</html>
------------------------------------------
@@ -128,3 +162,24 @@ class MainController < ApplicationController
end
------------------------------------------
+
+==== flash.now ====
+
+By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the `create` action fails to save a resource and you render the `new` template directly, that's not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use `flash.now` in the same way you use the normal `flash`:
+
+[source, ruby]
+------------------------------------------
+class ClientsController < ApplicationController
+
+ def create
+ @client = Client.new(params[:client])
+ if @client.save
+ # ...
+ else
+ flash.now[:error] = "Could not save client"
+ render :action => "new"
+ end
+ end
+
+end
+------------------------------------------