如何在 ActiveRecordError 上使用 Rails-i18n 并进行关联?

发布于 2024-12-18 22:16:54 字数 691 浏览 2 评论 0原文

我正在尝试翻译 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

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

如何编写翻译“#{e.message}”的代码?

I'm trying to translate https://github.com/lifo/docrails/blob/master/activerecord/lib/active_record/associations.rb

In my controller file I have:

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

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

This is the translation file I use : https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/th.rb

How do I write code for translate "#{e.message}"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2024-12-25 22:16:54

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

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

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

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

You can use this in your en.yml file

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

Then change the rescue block with the following

flash[:error] = t("book.error") % {:e => e.message}
淡看悲欢离合 2024-12-25 22:16:54

我以前也遇到过同样的问题。

所以有两种解决方案;


一个。您可以通过手动指定“代码”来自行刷新翻译的错误

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

begin
  @book.destroy
rescue ActiveRecord::DeleteRestrictionError => e
  flash[:error]= I18n.t('en.activerecord.errors.messages.restrict_dependent_destroy')
end

b。或者你可以使用你正在使用的 gem rails-i18n

首先,您需要配置您的 book 模型:

has_many :children, dependent: :restrict_with_error

然后您可以做

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

if @book.destroy
  # show success message
else
  flash[:error] = resource.errors.messages[:base].join(', ')
  # should include the translated error message if you are using rails-i18n
end

我假设您正在使用 :restrict_with_exception 而不是 :restrict_with_error,只是想提供替代方案以防万一。

I was facing the same issue once before.

So there are two solutions;


a. Either you can flash the translated error yourself by manually specify the "code"

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

begin
  @book.destroy
rescue ActiveRecord::DeleteRestrictionError => e
  flash[:error]= I18n.t('en.activerecord.errors.messages.restrict_dependent_destroy')
end

b. Or you can use the gem rails-i18n which you are using anyway;

Fristly you need to config your book model:

has_many :children, dependent: :restrict_with_error

then you can just do

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

if @book.destroy
  # show success message
else
  flash[:error] = resource.errors.messages[:base].join(', ')
  # should include the translated error message if you are using rails-i18n
end

I assume you are using :restrict_with_exception instead of :restrict_with_error, just want to provide an alternative just in case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文