aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/actioncontroller/cookies.txt
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-10-16 22:13:06 +0200
committerPratik Naik <pratiknaik@gmail.com>2008-10-16 22:13:06 +0200
commit9cb5400871b660e2c6d1654346650f07bb52a0c0 (patch)
tree6cd292650cf80b25494cf2f800318f337517b732 /railties/doc/guides/actioncontroller/cookies.txt
parent517bc500ed95a84fd2aadff34fdc14cb7965bc6b (diff)
downloadrails-9cb5400871b660e2c6d1654346650f07bb52a0c0.tar.gz
rails-9cb5400871b660e2c6d1654346650f07bb52a0c0.tar.bz2
rails-9cb5400871b660e2c6d1654346650f07bb52a0c0.zip
Merge docrails
Diffstat (limited to 'railties/doc/guides/actioncontroller/cookies.txt')
-rw-r--r--railties/doc/guides/actioncontroller/cookies.txt30
1 files changed, 19 insertions, 11 deletions
diff --git a/railties/doc/guides/actioncontroller/cookies.txt b/railties/doc/guides/actioncontroller/cookies.txt
index a845e452b2..d451f3f7a6 100644
--- a/railties/doc/guides/actioncontroller/cookies.txt
+++ b/railties/doc/guides/actioncontroller/cookies.txt
@@ -2,22 +2,30 @@
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:
-TODO: Find a real-world example where cookies are used
-
[source, ruby]
-----------------------------------------
-class FooController < ApplicationController
-
- def foo
- cookies[:foo] = "bar"
- end
+class CommentsController < ApplicationController
- def display_foo
- @foo = cookies[:foo]
+ def new
+ #Auto-fill the commenter's name if it has been stored in a cookie
+ @comment = Comment.new(:name => cookies[:commenter_name])
end
- def remove_foo
- cookies.delete(:foo)
+ def create
+ @comment = Comment.new(params[:comment])
+ if @comment.save
+ flash[:notice] = "Thanks for your comment!"
+ if params[:remember_name]
+ # Remember the commenter's name
+ cookies[:commenter_name] = @comment.name
+ else
+ # Don't remember, and delete the name if it has been remembered before
+ cookies.delete(:commenter_name)
+ end
+ redirect_to @comment.article
+ else
+ render :action => "new"
+ end
end
end