2015-05-15 35 views
0

我有我覺得應該是一個簡單的問題,我一直無法解決。我試圖在Rails 4應用程序中呈現json數據時設置cookie。在呈現JSON時創建Cookie

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

我一直在嘗試使用ActionDispatch ::餅乾,一個餅乾變量以及各種其他的方法設定鍵/值,而Googleing,但似乎沒有任何工作,我已經看到了。有什麼建議麼?這是不可能的呈現json?

回答

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. 

這是正確的方式...

+0

我已經糾正了我的代碼,以配合你的,並重新啓動我的應用程序,但沒有什麼是出現。 – user2684405

+0

reseponse.set_cookie(key,value)似乎對我有用。 – user2684405

+0

很棒... :) @ user2684405 – Milind