渲染时创建 Cookie JSON
Creating Cookies while rendering JSON
我有一个我觉得应该是一个简单的问题,但我一直无法解决。我正在尝试在 Rails 4 应用程序中呈现 json 数据时设置 cookie。
def index
request.cookies[:hi] = 'hello'
render json: User.all
end
我一直在尝试使用 ActionDispatch::Cookies、在 cookie 变量中设置 key/values 以及我在 Google 搜索时看到的各种其他方法,但似乎没有任何效果。有什么建议么?这不可能渲染吗json?
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.
这是正确的方法...
我有一个我觉得应该是一个简单的问题,但我一直无法解决。我正在尝试在 Rails 4 应用程序中呈现 json 数据时设置 cookie。
def index
request.cookies[:hi] = 'hello'
render json: User.all
end
我一直在尝试使用 ActionDispatch::Cookies、在 cookie 变量中设置 key/values 以及我在 Google 搜索时看到的各种其他方法,但似乎没有任何效果。有什么建议么?这不可能渲染吗json?
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.
这是正确的方法...