2017-02-22 40 views
1

我嘗試在我的聯絡頁上顯示Flash訊息後。這裏是我的代碼Rails 4-未顯示Flash訊息

def contact_us 
    UserMailer.delay.contact_email(params) 
    flash[:notice] = 'Thanks for contacting us!!!!!' 
    redirect_to contact_path 
end 

def contact 
end 

我試圖訪問Flash消息,但得到一個空白的響應。

[1] pry(#<#<Class:0x0000000aef74f0>>)> flash 
=> #<ActionDispatch::Flash::FlashHash:0x0000000a633328 @discard=#<Set: {}>, @flashes={}, @now=nil> 

也試過this解決方案但得到同樣的問題。 任何人都可以有一個想法,爲什麼我得到這個問題? 我該如何解決這個問題。

請求細節

Redirected to http://localhost:3000/contact 

Completed 302 Found in 213ms (ActiveRecord: 46.9ms) 
Started GET "/contact" for 127.0.0.1 at 2017-02-23 01:08:43 +0530 

編輯 我也試圖與

  • Try1

    redirect_to contact_path, notice: 'Thanks for contacting....' 
    

    仍然得到同樣的問題。

  • 嘗試2

flash[:notice] = 'Thanks for contacting....' flash.keep(:notice) redirect_to contact_path

仍然得到同樣的問題。

試過flash[:notice] doesn't work with redirect_to的所有解決方案仍然出現相同的錯誤。

我使用的是ruby 2.0.0p643和Rails 4.0.13。

+0

什麼應用程序日誌中說,在下面的錯誤?我假設你正在調用'contact_us',它被重定向到'contact',它應該顯示flash消息?這將有助於把這個問題... – phoet

+0

@phoet我已經添加了我的請求詳細信息。 – Natsu

+1

嘗試'flash.now [:notice] =「...」' –

回答

2

我得到了相同類型的問題。當我檢查服務器日誌中我得到了控制檯

Can't verify CSRF token authenticity 

因爲當你執行請求解決這個問題,我已經添加下面的行控制器

protect_from_forgery with: :null_session 
0

請嘗試以下。

def contact_us 
    UserMailer.delay.contact_email(params) 
    redirect_to contact_path 
    flash[:notice] = 'Thanks for contacting us!!!!!' 
end 
+0

感謝您的回答。但這不是指定Flash消息的正確方法。 – Natsu

+0

@Natsu嘗試在'contact_email'動作中移動你的Flash消息 –

0

添加到您的應用程序佈局:

<%= flash[:notice] %>