0

我对 Rails 很陌生,想知道最好的方法是什么:

我有一个控制器在数据库中创建记录。

如果发生特定的验证错误,我想设置一个标志,但我看不到使用我熟悉的 rails 模式完成此操作的好方法。

我要检测的模型验证是:

validates_uniqueness_of :title

我的控制器正在这样做:

fcs = Entity.create(:title => text)

当上述错误失败时,我可以使用 ActiveModel 错误集合。

我应该如何可靠地设置一个标志以编程方式指示标题已被占用?

到目前为止我考虑过

fcs.errors.messages.has_key?(:title)

但如果 title 因其他原因失败,这将返回 true。所以我需要更多类似的东西:

fcs.errors.messages[:title]==["has already been taken"]

但这将是一个令人头疼的维护问题,并且还会被不同的语言环境破坏......

那么有谁知道这应该如何使用 RoR 来完成?

感谢您的任何建议

编辑:建议标志“is_title_duplicated”的示例用法:

if(! fcs.errors.empty?)
      json['success']=false
      json['errors']=fcs.errors.full_messages
      json['title_was_duplicate'] = is_title_duplicated
      render :json => json

...
4

2 回答 2

2

我建议在你的模型类中添加一个方法来检测唯一性。

class Entity < ActiveRecord::Base
  def unique_title?
    Entity.where(:title => title).count > 0
  end
end

当然,这意味着您要运行该查询两次(一次用于 the validates_uniqueness_of,一次用于unique_title?)。只要性能可以接受,我更喜欢可读性而不是性能。如果性能不可接受,您仍然可以选择。unique_title?您可以在自己的自定义验证中重复使用并缓存结果。

class Entity < ActiveRecord::Base
  validate :title_must_be_unique

  def unique_title?
    # you may want to unset @unique_title when title changes
    if @unique_title.nil?
      @unique_title = Entity.where(:title => title).count > 0
    end
    @unique_title
  end

  private

  def title_must_be_unique
    unless unique_title?
      errors.add(:title, I18n.t("whatever-the-key-is-for-uniqueness-errors"))
    end
  end
end
于 2011-11-09T16:45:43.437 回答
1

你的意思是在记录上设置一个标志?每当验证失败时,记录就不会保存到数据库中

如果您只是想设置错误消息,则不必这样做。Rails 会自动将 fsc.erros 设置为类似于 {:title => "title has already beentaking"} 的哈希值。您可以通过将 :message 传递给您的验证来指定该消息。

此外,您可以使用 l18n 将消息国际化。只需按照此处所述编辑 yaml 文件:http: //guides.rubyonrails.org/i18n.html#configure-the-i18n-module

于 2011-11-09T16:28:01.367 回答