Reputation: 71
I have what I feel should be a simple question that I've been unable to solve. I'm trying to set a cookie while rendering json data in a Rails 4 app.
def index
request.cookies[:hi] = 'hello'
render json: User.all
end
I've been trying to use ActionDispatch::Cookies, setting key/values in a cookies variable and a variety of other methods I've seen while Googleing, but nothing seems to work. Any suggestions? Is this not possible rendering json?
Upvotes: 0
Views: 543
Reputation: 5112
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.
this is correct way...
Upvotes: 1