Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / actioncontroller_basics / cookies.txt
1 == Cookies ==
2
3 Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the `cookies` method, which - much like the `session` - works like a hash:
4
5 [source, ruby]
6 -----------------------------------------
7 class CommentsController < ApplicationController
8
9 def new
10 #Auto-fill the commenter's name if it has been stored in a cookie
11 @comment = Comment.new(:name => cookies[:commenter_name])
12 end
13
14 def create
15 @comment = Comment.new(params[:comment])
16 if @comment.save
17 flash[:notice] = "Thanks for your comment!"
18 if params[:remember_name]
19 # Remember the commenter's name
20 cookies[:commenter_name] = @comment.name
21 else
22 # Don't remember, and delete the name if it has been remembered before
23 cookies.delete(:commenter_name)
24 end
25 redirect_to @comment.article
26 else
27 render :action => "new"
28 end
29 end
30
31 end
32 -----------------------------------------
33
34 Note that while for session values, you set the key to `nil`, to delete a cookie value, you should use `cookies.delete(:key)`.