Ruby Cookie Usage

Source: Internet
Author: User
Tags require

Cookies in Ruby, like other programming languages, have expires,domain,path,secure,name these references, so let's look at a simple cookie instance.

Cookies and sessions are stored as strings, and other types of data types are coerced into strings, so if you want to save the object data in a cookie or session, you must first convert it to a bit stream form, and then turn it into an object form when you access it. So that you can pass object data in a cookie or session
#!/usr/bin/ruby

Require "CGI"
cgi = cgi.new ("Html4")
Cookie = cgi::cookie.new (' name ' => ' MyCookie '),
' Value ' => ' Zara Ali ',
' Expires ' => Time.now + 3600)
Cgi.out (' cookie ' => cookie) do
Cgi.head + cgi.body {"Cookie stored"}
End


Giving a cookie a method of assigning value


Class Hellocontroller < Applicationcontroller
def Set_cookie
Cookies[:my_name] = "AAAA"
Redirect_to:action => "Show_cookie"
End
def Show_cookie
My_name = Cookies[:my_name]
Render (: Text => "Hello #{my_name}!")
End
End

Output All Cookies

#!/usr/bin/ruby

Require "CGI"
cgi = cgi.new ("Html4")
Cookie = cgi.cookies[' MyCookie ']
Cgi.out (' cookie ' => cookie) do
Cgi.head + cgi.body {"Flavor:" + cookie[0]}
End

Attribute returned Value
Name Cookie Name
Value An array of cookies values
Path The cookie ' s path
Domain The domain
Expires The Expiration time (as a Time object)
Secure True If secure cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.