2015-07-21 32 views
1

當更新我的模型時出錯時,我正在渲染:編輯,但這是從我的url中剝離/edit,因爲#update#show具有不同的請求方法。爲了解決這個問題,我試着按照給出的建議here,但是當我嘗試提交無效表單時,這導致我得到一個ActionDispatch::Cookies::CookieOverflow錯誤。我應該如何正確地重新呈現編輯頁面,同時保持url和flash消息的/edit?是否可以檢查有效性並顯示錯誤而無需致電更新?保持模型在重定向編輯時出現錯誤

原始代碼:

def edit 
end 

def update 
    respond_to do |format| 
    format.html do 
     if @model.update(model_params) 
     redirect_to home_base_url_or_default(model_url(@model)), notice: "Successfully updated." 
     else 
     render :edit 
     end 
    end 
    end 
end 

失敗代碼:

def edit 
    if flash[:model] 
    @model = flash[:model] 
    end 
end 

def update 
    respond_to do |format| 
    format.html do 
     if @model.update(model_params) 
     redirect_to home_base_url_or_default(model_url(@model)), notice: "Successfully updated." 
     else 
     flash[:model] = @model 
     redirect_to :action => :edit 
     end 
    end 
    end 
end 
+0

rails中的CookieStore大小爲4KB。只要超出限制,就會引發錯誤。我鼓勵你檢查這個[鏈接](http://stackoverflow.com/questions/9473808/cookie-overflow-in-rails-application)。也許它會有所幫助 –

回答

0

一種方式做到這一點是允許的edit行動接受POST方法爲好。使用request.method來檢查它是否是POSTGET,然後相應地執行您的renderredirect

1

在這種情況下,通過執行渲染,然後通過在控制器中設置實例var來控制視圖,以確定問題是否是編輯頁面,而不是做重定向。也可以在CSS中使用update類。但是,這仍然具有展示頁面的網址,但至少佈局是正確的。

相關問題