2012-09-15 26 views
1

當試圖重定向POST請求時,我發現rails的flash哈希已被清除。在ruby-on-rails中重定向POST請求導致flash哈希被刪除

當初始請求是GET時,重定向工作正常。

在配置

/routes.rb中

post 'test/hello' 
#get 'test/hello' #uncomment this line and comment out the post and the flash is preserved 
get 'test/goodbye' 
在app /控制器/ test_controller.rb

def hello 
    respond_to do |format| 
     format.html do 
      redirect_to movies_goodbye_url, :notice => "I disappear on post requests" 
     end 
    end 
end 

def goodbye 
end 
在app

/視圖/測試/ goodbye.html.haml

%h1 Test#goodbye 
%p#alert= alert 
%p#notice= notice 

我想要這個動作來處理來自表單提交的帖子,這些提交將錯誤情況重定向到我現有的視圖之一。我對rails很新,所以如果有更好的方法來處理這個用例,請讓我知道。

回答

2

嘗試增加在那裏flash.keep

flash.keep(:notice) 
redirect_to movies_goodbye_url, :notice => "I disappear on post requests" 

參見:Flash message in redirect not working

+0

+1大有趕超!但是爲什麼Rails以這種方式工作?這應該作爲一個錯誤提交給他們的錯誤跟蹤器? –