我正在尝试本地化我的rails应用程序(webservice)。我安装了创业板“rails-i18n”,运行良好。
但它不翻译ActiveRecord::RecordNotFound消息。在rails代码中:methods.rb
raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"信息似乎是硬编码的。
除了不使用Model.find_by_‘属性之外,还有其他解决方案吗?
发布于 2013-12-30 09:24:02
我有足够的理由告诉用户更多的信息:
exception.message.match /^Couldn't find (\w+) with (id=([\S]*))?/
msg = t 'activerecord.exceptions.not_found', klass: $1, id: $3既然我想要捷克人的交易:
cs:
activerecord:
exceptions:
not_found: "Nelze nalézt %{klass} s id=%{id}"..when是硬编码的,唯一的方法就是硬去编码它。
发布于 2015-07-27 12:09:56
YAML配置的另一种方式:
en:
activerecord:
exceptions:
not_found: "%{model_name} not found"还可以为模型名称设置复数形式,添加如下:
en:
activerecord:
models:
user:
one: Dude
other: Dudes发布于 2013-10-23 09:58:29
您可以在应用程序控制器中捕获RecordNotFound异常并返回本地化消息。通过这种方式,您还可以更改404的默认行为:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
private
def not_found
render :json => { :message => I18n.t('exception.record_not_found') },
:status => :not_found
end
endhttps://stackoverflow.com/questions/19538272
复制相似问题