2014-04-22 33 views
0

我在控制器很高興設置閃光燈的消息是這樣的:有沒有可以在rails中設置'全局'flash消息的地方?

redirect_to root_path, error: 'This is a highly eloquent and specialised error message just for you, my iridescent, transcendent and all round marvellous bundle of joy! You are not allowed in here you cheeky chappy, you!' 

不過,我覺得我做了很多複製粘貼的,當涉及到刻錄功能規格來測試這些郵件是渲染:

context 'when user has eaten all the pies' do 
    let(:cheeky) { FactoryGirl.build(:cheeky_user) } 
    subject { page } 

    before do 
    log_in cheeky 
    click_link 'steal pies' 
    end 

    it_should_behave_like 'home page' 
    it{ should have_selector('.error', text: 'This is a highly eloquent and specialised error message just for you, my iridescent, transcendent and all round marvellous bundle of joy! You are not allowed in here you cheeky chappy, you!') } 
end 

它可以是一個痛苦時斷言,因爲我缺少一個字符會失敗,但越是這樣,當得到一堆因爲我已經調整了一些錯誤信息沒有規範的。

測試錯誤消息這可能是一個脆弱的規範的例子,但我想測試顯示正確的錯誤消息。

我真的希望能夠做到這一點:

#somewhere in rails 
ERROR_MSG_23 = 'This is a highly eloquent and specialised error message just for you, my iridescent, transcendent and all round marvellous bundle of joy! You are not allowed in here you cheeky chappy, you!' 

#controller 
redirect_to root_path, error: ERROR_MSG_23 

#spec 
context 'when user has eaten all the pies' do 
    let(:cheeky) { FactoryGirl.build(:cheeky_user) } 
    subject { page } 

    before do 
    log_in cheeky 
    click_link 'steal pies' 
    end 

    it_should_behave_like 'home page' 
    it{ should have_selector('.error', text: ERROR_MSG_23) } 
end 

是否有寶石的呢?或者這可以在香草欄中實施?

回答

2

您可以使用I18n,像這樣:

en.yml:

en: 
    flash_messages: 
    message_1: 'This is a highly eloquent and specialised error message just for you, my iridescent, transcendent and all round marvellous bundle of joy! You are not allowed in here you cheeky chappy, you!' 

控制器:

redirect_to root_path, error: t('flash_messages.message_1') 

規格:

it{ should have_selector('.error', text: I18n.t('flash_messages.message_1') } 
相關問題