B
B
Bogdan2018-02-27 15:36:27
Ruby on Rails
Bogdan, 2018-02-27 15:36:27

How to add a cookie to the response?

Hello. Can you please tell me how to make the response have cookies?
The settings are config.api_only = true
here is an example

def test_cookie
    cookies[:cookie_name1] = 'cookie_value1'
    cookies[:cookie_name22] = 'cookie_value22'
    
    render json: { status: true }
  end

Added to application_controller.rb
class ApplicationController < ActionController::API
  include ActionController::Cookies
end

application.rb
module RailsApiDemo
  class Application < Rails::Application
    config.load_defaults 5.1
    config.api_only = true
  end
end

Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Demidenko, 2018-02-28
@Dem1

# config/application.rb
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, key: '_coookie_name', expire_after: 30.days

D
Dmitri Sinitsa, 2018-02-27
@unabl4

No way. Middleware for sessions and cookies is disabled in API mode.
Why are there cookies in the API? Something is wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question