2013-05-06 104 views
0

我有一個控制器,可以根據該控制器創建一個包含我正在開發的遊戲信息的cookie。我遇到的問題是,當我在該控制器中使用另一個操作時,值發生了變化。檢查簽名cookie是否存在

這是我的控制器:

def new 
     @videos = Video.order("RANDOM()").limit(2) 
     if !cookies.signed[:game] 
       cookies.signed[:game] = { 
        :value => @videos, 
        :domain => 'localhost', 
        :secure => !(Rails.env.test? || Rails.env.development?) 
       } 
     end 
    end 

    def start_game 
     respond_to do |format| 
      format.js 
     end 
    end 

start_game.erb.js

console.log('<%= cookies.signed[:game].first.titel %>') # This should print out the same value but it doesn't do that. 

new.html.erb

... 
<%= button_to "game", { :action => "start_game" }, { :remote => true, :form_class => "test_button" } %> 

我可以檢查whetever簽署的cookie存在與否?

回答

0

你正在存儲的東西不是一組視頻,而是一個ActiveRecord::Relation,簡而言之,一個未評估的查詢。相反,您希望執行new操作中的查詢並存儲結果。

最簡單的方法是不是寫

@videos = Video.order("RANDOM()").limit(2).all