2011-04-30 53 views
12

即使我敢肯定,我知道爲什麼這個錯誤被提出,似乎我不知道爲什麼或怎麼我的會話超過4KB限制...幫助調試我的會話? Rails 3塊中ActionDispatch ::餅乾:: CookieOverflow

我的應用程序工作正常,但一旦我故意開始添加錯誤以查看我的事務是否回滾,我開始出現此錯誤。

爲了給出一些背景知識,我正在忙着編寫一個錦標賽應用程序(在本節中)來創建錦標賽,然後根據錦標賽隊伍的數量添加一些錦標賽腿以及用一些「幽靈」填充錦標賽一旦腿已經被創建,燈具就會被關閉。

flash [:tournament]之前正常工作;使用錦標賽對象,我可以訪問任何AR驗證錯誤以及在上一頁輸入的用於創建錦標賽的數據。

TournamentController.rb

begin 
    <other code> 
    Tournament.transaction do 
    tournament.save! 
    Tournament.generate_legs tournament 
    Tournament.generate_ghost_fixtures tournament 
    end 

    flash[:notice] = "Tournament created!" 
    redirect_to :action => :index 
rescue Exception => e 
    flash[:tournament] = tournament 
    redirect_to :action => :new, :notice => "There was an error!" 
end 

Tournament.rb

self.generate_ghost_fixtures(tournament) 
    <other code> 
    #Generate the ghost fixtures 
    #tournament_legs is a has_many association 
    tournament_legs_array = tournament.tournament_legs 

    tournament_legs_array.each do |leg| 
    number_of_fixtures = matches[leg.leg_code] 

    #For the first round of a 32 team tournament, this block will run 16 times to create the matches 
    number_of_fixtures.times do |n| 
     Fixture.creatse!(:tournament_leg_id => leg.id, :match_code => "#{leg.leg_code}-#{n+1}") 
    end 
    end 
end 

我能做的只是猜測,爲什麼我的會話變量超過4KB? 我有可能通過flash變量傳遞的錦標賽對象也包含所有關聯嗎?

這裏是我的會話轉儲一旦我得到錯誤。

希望這是足夠的信息來幫助我:)

感謝

會議自卸

_csrf_token: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
flash: {:tournament=>#<Tournament id: nil, tournament_name: "asd", tournament_description: "asdasd", game_id: 1, number_of_teams: 16, start_date: "2011-04-30 00:00:00", tournament_style: "single elimination", tournament_status: "Drafting", active: true, created_at: "2011-04-30 10:07:28", updated_at: "2011-04-30 10:07:28">} 
player_id: 1 
session_id: "4e5119cbaee3d5d09111f49cf47aa8fa" 
+0

看來,在救援條款中的flash [:tournament] =錦標賽是罪魁禍首。 – Roman 2011-05-07 08:42:04

+1

是的,這是罪魁禍首,我想知道的是,地獄是如何超過4KB ...:D協會?我不知道。 – 2011-05-23 18:47:01

回答

13

關於依存關係,這是可能的。在會話中保存ActiveRecord實例也不是推薦的方法。您應該只保存該ID。如果您在所有請求中都需要使用之前的過濾器進行檢索。

你可以閱讀更多的爲什麼是一個壞主意:在session_store.rb http://asciicasts.com/episodes/13-dangers-of-model-in-session

+1

是的,我知道將大對象作爲會話變量保存並不明智,有點想知道更多關於我發現自己的獨特情況...... – 2011-05-23 18:46:07

2

,以取消對最後一行:active_record_store 現在重新啓動服務器

+0

並未真正回答這個問題......但在某些情況下仍然有用你想使用數據庫來存儲。 – courtsimas 2013-03-28 21:59:02

2

我將異常轉換成字符串賦值前它用'to_s'來刷新[:比賽]。 我有同樣的錯誤,它似乎分配一個異常對象的會議變種像閃光意味着它將整個堆棧跟蹤與它進入會議。試試吧,爲我工作。

+1

感謝Peter的迴應,我會對此表示敬意! – 2012-06-29 14:02:33

3

通常被接受和推薦的方法是而不是錯誤時使用重定向,而是直接渲染。標準的「控制式」是這樣的:

def create 
    @tournament = Tournament.new(params[:tournament]) 
    if @tournament.save 
    redirect ... 
    else 
    render 'new' # which will have access to the errors on the @tournament object and any other instance variable you may define 
    end 
end 

class Tournament < ActiveRecord::Base 
    before_create :set_up_legs 
end 

在保存成功,你可以放下所有的實例變量(從而掃除了內存狀態),並重定向到另一頁。失敗(或例外)時,將對象保留在內存中,並呈現視圖模板(通常爲「新建」或「編輯」表單頁面)。如果您使用標準的Rails驗證和錯誤處理,那麼該對象將會有一個只能顯示的錯誤數組。

我還建議您使用自動爲您提供事務的ActiveRecord關聯。如果將所有這些都推入模型中,例如一個「set_up_legs」方法什麼的,那麼你可以使用ActiveRecord錯誤處理。這是"skinny controller, fat model"範式的一部分。

+0

很抱歉,對於您回覆的回覆晚了,我同意您所說的關於更好實踐的一切。我也明白爲什麼使用這些更好的做法會完全避免發生這種錯誤。然而,我仍然在爲了cookie的可能性超過4kB而撓頭。我想明白爲什麼會發生這種情況。再次感謝。附:真的很喜歡你在RSpec上的屏幕錄像。乾杯! – 2012-06-29 13:54:34