Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to know how to disable cookies in Rails 5.1

I have a small app which is used by company staff only. I don't use any 'cookies/session' information from Rails directly. Given current requirements to request acceptance for cookies etc. I would like to completely disable cookies so that Rails doesn't send ANY cookies to the browser with any responses I generate.

like image 370
nexar Avatar asked Nov 01 '17 13:11

nexar


People also ask

How do I delete cookies in Rails?

Description: Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past. Like []=, you can pass in an options hash to delete cookies with extra data such as a :path.

Where are Rails cookies stored?

Cookies are stored in the browser. The browser doesn't care about what's in the cookies you set. It just stores the data and sends it along on future requests to your server. You can think of them as a hash—and indeed, as we'll see later, Rails exposes cookies with a method that behaves much like a hash.

What is cookies in Ruby on Rails?

Cookies, Sessions and Flashes are three special objects that Rails gives you in which each behave a lot like hashes. They are used to persist data between requests, whether until just the next request, until the browser is closed, or until a specified expiration has been reached.


1 Answers

To disable cookies completely, use this inside application.rb:

config.middleware.delete ActionDispatch::Cookies
config.middleware.delete ActionDispatch::Session::CookieStore

and in config/initializers/session_store.rb:

Rails.application.config.session_store :disabled

You can find more details at this blog post: http://www.glitchwrks.com/2017/01/16/removing-cookies-sessions-rails-5

Btw, if you need to disable cookies only for some controllers/actions you can use this:

    after_action -> { request.session_options[:skip] = true }
like image 124
The Omitter Avatar answered Oct 19 '22 13:10

The Omitter