How do I set a cookie with a (ruby) rack middleware component?

前端 未结 2 548
轮回少年
轮回少年 2021-02-04 11:13

I\'m writing a rack middleware component for a rails app that will need to conditionally set cookies. I am currently trying to figure out to set cookies. From googling around

2条回答
  •  滥情空心
    2021-02-04 11:48

    You can also use the Rack::Utils library to set and delete headers without creating a Rack::Response object.

    class RackApp
      def initialize(app)
        @app = app
      end
    
      def call(env)
        status, headers, body = @app.call(env)
    
        Rack::Utils.set_cookie_header!(headers, "foo", {:value => "bar", :path => "/"})
    
        [status, headers, body]
      end
    end
    

提交回复
热议问题