如何使用(ruby)机架中间件组件设置 cookie?

发布于 2024-09-10 20:47:48 字数 423 浏览 14 评论 0原文

我正在为 Rails 应用程序编写一个机架中间件组件,该组件需要有条件地设置 cookie。我目前正在尝试设置cookies。通过谷歌搜索,这似乎应该可行:

class RackApp
  def initialize(app)
    @app = app
  end

  def call(env)
    @status, @headers, @response = @app.call(env)
    @response.set_cookie("foo", {:value => "bar", :path => "/", :expires => Time.now+24*60*60})
    [@status, @headers, @response]
  end
end

它不会给出错误,但也不会设置 cookie。我做错了什么?

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 it seems like this should work:

class RackApp
  def initialize(app)
    @app = app
  end

  def call(env)
    @status, @headers, @response = @app.call(env)
    @response.set_cookie("foo", {:value => "bar", :path => "/", :expires => Time.now+24*60*60})
    [@status, @headers, @response]
  end
end

which doesn't give errors, but doesn't set a cookie either. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-09-17 20:47:48

如果要使用 Response 类,则需要根据调用堆栈中更下方的中间件层的结果来实例化它。
另外,您不需要像这样的中间件的实例变量,并且可能不想以这种方式使用它们(@status 等将在请求提供后保留在中间件实例中)

class RackApp
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
    # confusingly, response takes its args in a different order
    # than rack requires them to be passed on
    # I know it's because most likely you'll modify the body, 
    # and the defaults are fine for the others. But, it still bothers me.

    response = Rack::Response.new body, status, headers

    response.set_cookie("foo", {:value => "bar", :path => "/", :expires => Time.now+24*60*60})
    response.finish # finish writes out the response in the expected format.
  end
end

如果您知道自己在做什么如果您不想实例化新对象,可以直接修改 cookie 标头。

If you want to use the Response class, you need to instantiate it from the results of calling the middleware layer further down the stack.
Also, you don't need instance variables for a middleware like this and probably don't want to use them that way(@status,etc would stay around in the middleware instance after the request is served)

class RackApp
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
    # confusingly, response takes its args in a different order
    # than rack requires them to be passed on
    # I know it's because most likely you'll modify the body, 
    # and the defaults are fine for the others. But, it still bothers me.

    response = Rack::Response.new body, status, headers

    response.set_cookie("foo", {:value => "bar", :path => "/", :expires => Time.now+24*60*60})
    response.finish # finish writes out the response in the expected format.
  end
end

If you know what you are doing you could directly modify the cookie header, if you don't want to instantiate a new object.

强辩 2024-09-17 20:47:48

您还可以使用 Rack::Utils 库来设置和删除标头,而无需创建 Rack::Response 对象。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文