diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-10-21 18:33:40 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-10-21 18:33:40 +0100 |
commit | a03e2b356c66ddc8809fa2b23a2a7d652f173b8b (patch) | |
tree | ab8552913475bf94a78e4cbfbae804b2ecd9eca2 /railties/doc/guides/source/actioncontroller_basics/cookies.txt | |
parent | 18542c9e00209679bdaacf64075819fb887ec856 (diff) | |
download | rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.tar.gz rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.tar.bz2 rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.zip |
Merge with docrails. Also add a rake task to generate guides in your rails application :
rake doc:guides
The rake task will generate guides inside doc/guides directory of your application. Open index.html to browse.
Diffstat (limited to 'railties/doc/guides/source/actioncontroller_basics/cookies.txt')
-rw-r--r-- | railties/doc/guides/source/actioncontroller_basics/cookies.txt | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/railties/doc/guides/source/actioncontroller_basics/cookies.txt b/railties/doc/guides/source/actioncontroller_basics/cookies.txt new file mode 100644 index 0000000000..d451f3f7a6 --- /dev/null +++ b/railties/doc/guides/source/actioncontroller_basics/cookies.txt @@ -0,0 +1,34 @@ +== Cookies == + +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: + +[source, ruby] +----------------------------------------- +class CommentsController < ApplicationController + + 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 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 +----------------------------------------- + +Note that while for session values, you set the key to `nil`, to delete a cookie value, you use `cookies.delete(:key)`. |