Cookies Management in Ruby on Rails

Submitted by Karthikeyan on

Example #1:
This program sets cookie (customer) and reads.

Controller


class LookController < ApplicationController
  def index
    set
    render :action=>"set"
  end
  def read
    @cookie_data = cookies[:customer]
  end
  def set
    cookies[:customer]={:vaule=>"xxxxx", :expires=>31.days.from_now}
  end
end

Views


set.html.erb

Setting a cookie

The cookie was set click <%=link_to "here", :action=>"read"%> read the cookie
read.html.erb

Reading a cookie

The cookie says the customer is <%=@cookie_data%>