如何将ActiveRecordError上的rails-i18n与关联一起使用?

use*_*435 5 localization ruby-on-rails ruby-on-rails-3

我正在尝试翻译https://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/associations.rb

在我的控制器文件中,我有:

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

begin
  @book.destroy
rescue ActiveRecord::DeleteRestrictionError => e
  flash[:error]= e.message # <<< Translate this message ?
end
Run Code Online (Sandbox Code Playgroud)

这是我使用的翻译文件:https : //github.com/svenfuchs/rails-i18n/blob/master/rails/locale/th.rb

我该如何编写代码translate "#{e.message}"

raj*_*ith 1

en.yml您可以在您的文件中使用它

activerecord:
   book:
    error: 'book error: %{e}'
Run Code Online (Sandbox Code Playgroud)

然后使用以下内容更改救援块

flash[:error] = t("book.error") % {:e => e.message}
Run Code Online (Sandbox Code Playgroud)