
- Ruby on Rails Tutorial
- Ruby on Rails - Home
- Ruby on Rails - Introduction
- Ruby on Rails - Installation
- Ruby on Rails - Framework
- Ruby on Rails - Directory Structure
- Ruby on Rails - Examples
- Ruby on Rails - Database Setup
- Ruby on Rails - Active Records
- Ruby on Rails - Migrations
- Ruby on Rails - Controllers
- Ruby on Rails - Routes
- Ruby on Rails - Views
- Ruby on Rails - Layouts
- Ruby on Rails - Scaffolding
- Ruby on Rails - AJAX
- Ruby on Rails - File Uploading
- Ruby on Rails - Send Emails
- Ruby on Rails Resources
- Ruby on Rails - References Guide
- Ruby on Rails - Quick Guide
- Ruby on Rails - Resources
- Ruby on Rails - Discussion
- Ruby Tutorial
- Ruby Tutorial
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ruby on Rails - Session and Cookies
Sessions
To save data across multiple requests, you can use either the session or the flash hashes. A flash stores a value (normally text) until the next request, while a session stores data during the complete session.
session[:user] = @user flash[:message] = "Data was saved successfully" <%= link_to "login", :action => 'login' unless session[:user] %> <% if flash[:message] %> <div><%= h flash[:message] %></div> <% end %>
It's possible to turn off session management −
session :off # turn session management off session :off, :only => :action # only for this :action session :off, :except => :action # except for this action session :only => :foo, # only for :foo when doing HTTPS :session_secure => true session :off, :only=>:foo, # off for foo,if uses as Web Service :if => Proc.new { |req| req.parameters[:ws] }
Check out link for more detail on Session Management
Cookies
Following is the syntax for setting cookies −
# Set a simple session cookie cookies[:user_name] = "david" # Set a cookie that expires in 1 hour cookies[:login] = { :value => "XJ12", :expires => Time.now + 3600}
Following is the syntax for reading cookies −
cookies[:user_name] # => "david" cookies.size # => 2
Following is the syntax for deleting cookies −
cookies.delete :user_name
All the option symbols for setting cookies are −
value − The cookie.s value or list of values (as an array).
path − The path for which this cookie applies. Defaults to the root of the application.
domain − The domain for which this cookie applies.
expires − The time at which this cookie expires, as a +Time+ object.
secure − Whether this cookie is a secure cookie or not (default to false). Secure cookies are only transmitted to HTTPS servers.
Check out the link on Cookies Management, for more detail.