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 |