Validation 翻译类I18n::Backend::ActiveRecord::Translation Rails的验证

Validation 翻译类I18n::Backend::ActiveRecord::Translation Rails的验证,validation,activerecord,ruby-on-rails-4,Validation,Activerecord,Ruby On Rails 4,我使用gemi18n-active_记录翻译我网站中的数据,并将翻译保存在数据库中。我已授予管理员编辑翻译的权限。一切正常 但是这个更新数据的错误部分是管理员可以在没有任何可能破坏应用程序的数据值的情况下更新翻译,所以我希望 此类的验证 如果我像这样使用 class Translation < ActiveRecord::Base validates :value, :presence => { :message => "Please enter your value" }

我使用gemi18n-active_记录翻译我网站中的数据,并将翻译保存在数据库中。我已授予管理员编辑翻译的权限。一切正常

但是这个更新数据的错误部分是管理员可以在没有任何可能破坏应用程序的数据值的情况下更新翻译,所以我希望 此类的验证

如果我像这样使用

class Translation < ActiveRecord::Base
  validates :value, :presence => { :message => "Please enter your value" }
end
它不起作用

即使我尝试过使用回调和作用域,它们似乎都不起作用


请提供任何解决方案或参考以完成此操作。

您是否尝试将此约束直接放到值表字段中

这就是它在这里的表现:

>> class Translation < ActiveRecord::Base
>>   validates :value, :presence => { :message => "Please enter your value" }
>>   end
=> {:presence=>{:message=>"Please enter your value"}}
>> Translation.new.tap { |t| p t.valid?; p t.errors }
false
#<ActiveModel::Errors:0x007fce0f11aa78 @base=#<Translation id: nil, value: nil>, @messages={:value=>["can't be blank", "Please enter your value", "Please enter your value"]}>
=> #<Translation id: nil, value: nil>
>> Translation.new(value: "").tap { |t| p t.valid?; p t.errors }
false
#<ActiveModel::Errors:0x007fce0f0f3798 @base=#<Translation id: nil, value: "">, @messages={:value=>["can't be blank", "Please enter your value", "Please enter your value"]}>
=> #<Translation id: nil, value: "">
>> Translation.new(value: "something").tap { |t| p t.valid?; p t.errors }
true
#<ActiveModel::Errors:0x007fce0f0cb540 @base=#<Translation id: nil, value: "something">, @messages={}>
=> #<Translation id: nil, value: "something">

嗨,弗兰克,不,它在工作。我也试过,但没有成功。如果该类是从activemodel继承的,那么答案很好,这里是另一种情况。如果我们使用gemi18n-active\u记录,转换类将从i18n::Backend::ActiveRecord::translation继承。我在控制台中尝试了与您的代码类似的方法,我得到的输出如下所示:Translation.newvalue:.tap{t|t.valid?;p t.errors}true查看完整的错误报告Translation.new.tap{t|t.valid?;p t.errors}true Translation.newvalue:.tap{t|t.valid?;p t.errors}true,@messages={}>Translation.newvalue:something.tap{| t | pt.valid?;pt.errors}true FWIW.。您能粘贴I18n::Backend::ActiveRecord::Translation.concenters的结果吗?