在渲染JSON时创建Cookie

时间:2015-05-15 17:36:31

标签: ruby-on-rails ruby-on-rails-4 cookies

我觉得应该是一个我无法解决的简单问题。我正在尝试在Rails 4应用程序中渲染json数据时设置cookie。

def index
   request.cookies[:hi] = 'hello'
   render json: User.all
end

我一直在尝试使用ActionDispatch :: Cookies,设置Cookie变量中的键/值以及我在谷歌时看到的各种其他方法,但似乎没有任何效果。有什么建议?这不可能渲染json吗?

1 个答案:

答案 0 :(得分:1)

    def index
       cookies[:hi] = "hello" 
       render json: User.all
    end

# Set a cookie that expires in 1 hour
cookies[:login] = { :value => "XJ12", :expires => Time.now + 3600}
# delete cookie
cookies.delete :login




All the option symbols for setting cookies are:

value - the cookie.s value or list of values (as an array).
path - the path for which this cookie applies. Defaults to the root of the application.
domain - the domain for which this cookie applies.
expires - the time at which this cookie expires, as a +Time+ object.
secure - whether this cookie is a secure cookie or not (default to false). Secure cookies are only transmitted to HTTPS servers.

这是正确的方法......