Open In App

How to Set Cookie in Ruby on Rails?

Cookies are small pieces of data that are sent from a website and stored on the user's computer by the user's web browser. They are commonly used for session management, user authentication, personalization, and tracking.

In Ruby on Rails, you can use the 'cookies' object to set, read, and delete cookies within your Rails application. It is commonly used in controllers to create the server-side logic, but you can use it in views too.

Steps to Set Cookies in Ruby on Rails

Step 1: Create a demo project using the command below. It will create a project named 'myapp' in the current directory. Then use the second command to get into your project directory.

rails new myapp

cd myapp

Create demo project

create a demo project

Step 2: Next, generate a controller and a view(web page) using the command below. It will create a controller file 'home_controller.rb' in ‘app/controllers’ and corresponding view file 'index.html.erb' which is our webpage in ‘app/views/home’. It will also create a route in ‘app/config/routes.rb’.

rails generate controller home index

Generate Controller

generate controller and view

Step 3: Now, configure the root to access the index page every time you run the server. Open 'config/routes.rb' and add the following line.

root 'home#index'

Configure the root

Configure routes

Step 4: Now, open 'app/controllers/home_controller.rb' and add the following code to set a cookie in the 'index' action.

The cookie will be set when the corresponding action in your controller is executed. In our case, the cookie will be set when the 'index' action of the 'HomeController' is invoked. In simple words, when someone accesses the home page of our application, the code will set the cookie.

class HomeController < ApplicationController
  def index
    cookies[:name] = {
      value: "GeeksforGeeks",
      expires: 1.week.from_now
    }
  end
end

This code sets a cookie named 'user_id' with the value "123", and it will expire in 1 week. You can specify other parameters also such as ':domain' , ':path' , ':secure' , ':httponly' and 'tld_length'.

  1. ':domain': The ':domain' option allows you to specify the domain for which the cookie is valid. For example, setting 'domain : .geeksforgeeks.org' would make the cookie accessible to all subdomains of geeksforgeeks.org.
  2. ':path': The path for which this cookie applies. The default is the root of the application('/').
  3. ':secure': When you set the ':secure' option to true, the cookie will only be sent by the browser over HTTPS connections. The default value is false.
  4. ':httponly': When you set the ':httponly' option to true prevents the cookie from being accessed via JavaScript. Then it can be accessed using HTTP only. The default value is false.
  5. ':tld_length': Top level domains consist of multiple parts (eg. .co.in, .com.us). ':tld_length' is used with ':domain' to determine how many parts of the domain name should be considered as the TLD. The default value is 1.

Step 5: Now, Write a message in 'app/views/home/index.html.erb' which will be displayed you access the home page.

<!DOCTYPE html>
<html>
<head>
  <title>GeeksforGeeks</title>
</head>
<body>
  <h1>Cookie Set Successfully</h1>
</body>
</html>

Step 6: Finally, start the Rails server to see the output. After executing the command, open 'http://localhost:3000' in your browser.

rails server

Output:

Output

Now, to check if the cookie is set or not you can go to 'http://localhost:3000', right-click and select 'inspect'. Navigate to 'Application' tab, expand the 'Cookies' section in 'Storage' and you can see your cookies.

View cookies

Here you can see the value of cookie which is 'GeeksforGeeks' and it will expire in 1 week.

Article Tags :