2011-11-30 22 views
4

我試着翻譯https://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/associations.rb如何在ActiveRecordError上使用rails-i18n進行關聯?

在我的控制文件中我有:

@book = Book.find(params[:id]) 

begin 
    @book.destroy 
rescue ActiveRecord::DeleteRestrictionError => e 
    flash[:error]= e.message # <<< Translate this message ? 
end 

這是翻譯文件我用:https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/th.rb

我怎樣寫代碼translate "#{e.message}"

回答

1

您可以在en.yml文件

activerecord: 
    book: 
    error: 'book error: %{e}' 

使用和u可以用這個

flash[:error] = t("book.error") % {:e => e.message} 

這部作品在馬情況下CHANE烏爾救援塊

相關問題