如何在 ActiveRecordError 上使用 Rails-i18n 并进行关联?
我正在尝试翻译 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
en.yml
文件中使用它然后使用以下内容更改救援块
You can use this in your
en.yml
fileThen change the rescue block with the following
我以前也遇到过同样的问题。
所以有两种解决方案;
一个。您可以通过手动指定“代码”来自行刷新翻译的错误
b。或者你可以使用你正在使用的 gem
rails-i18n
;首先,您需要配置您的
book
模型:然后您可以做
我假设您正在使用
: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"
b. Or you can use the gem
rails-i18n
which you are using anyway;Fristly you need to config your
book
model:then you can just do
I assume you are using
:restrict_with_exception
instead of:restrict_with_error
, just want to provide an alternative just in case.