开发者

What's the Rails 3 replacement for ActiveRecord::Errors?

What's the Rails 3 replacement for ActiveRecord::Errors?

In Rails开发者_如何学JAVA 2.3.8, this is an object:

>> ActiveRecord::Errors
=> ActiveRecord::Errors

In Rails 3.0.0rc, you get a NameError:

>> ActiveRecord::Errors
NameError: uninitialized constant ActiveRecord::Errors
 from (irb):2

I'm trying to make the wizardly generator work with Rails 3.

$ rails g wizardly_scaffold home

But it fails:

/Library/Ruby/Gems/1.8/gems/wizardly_gt-0.1.8.9/lib/validation_group.rb:150:
uninitialized constant ActiveRecord::Errors (NameError)

The line it refers to is this:

ActiveRecord::Errors.send :include, ValidationGroup::ActiveRecord::Errors

Earlier in the file, we see:

module ValidationGroup
  module ActiveRecord
...
    module Errors # included in ActiveRecord::Errors
      def add_with_validation_group(attribute, msg = I18n.translate('activerecord.errors.messages')[:invalid], *args, &block)
        add_error = @base.respond_to?(:should_validate?) ? (@base.should_validate?(attribute.to_sym) || attribute == :base) : true
        add_without_validation_group(attribute, msg, *args, &block) if add_error
      end
...
end


That'd be ActiveModel::Errors. Things such as validations and error handling have been moved over to Active Model to provide a common API for all ORM Railties such as Active Record, Data Mapper, Mongoid etc. to hook into Rails with.

It would appear the wizardly plugin needs to check for ActiveModel first and if it exists, then include the error handling there rather than ActiveRecord::Errors. A trivial change.


Try this gem

http://rubygems.org/gems/wizardly_gt

I have only just begin playing with wizardly, but the above at least seems to be compatible with Rails 3.


Wizardly obviously does a lot more, but you should check out validation_scopes, which I just updated for Rails3 compatibility. Rather than grouping things by attributes, it just lets you explicitly declare different groups of validations by creating namespaced collections of errors. Internally it's a much simpler implementation (the same code handles Rails 2 and 3). Personally I find this to be more flexible than grouping by attribute (what if an attribute should have different constraints in different steps of a wizard for instance?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜