Skip to content

Instantly share code, notes, and snippets.

@nitsujw
Created September 24, 2009 18:31
Show Gist options
  • Save nitsujw/192938 to your computer and use it in GitHub Desktop.
Save nitsujw/192938 to your computer and use it in GitHub Desktop.
Problem: With a form_for in Rails 3, it checks object.errors['field'].any?
which in DataMapper returns nil if no errors are found,
in ActiveRecord it returns an empty array.
<% form_for @user, :url => "/users" do |f| %>
<%= f.text_field :login %>
<% end %>
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.any?
Error at actionpack/lib/action_view/helpers/active_model_helper.rb:285
if object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && object.errors[@method_name].any?
ActiveRecord:
>> a = Post.new
=> #<Post id: nil, title: nil>
>> a.errors['title']
=> []
>> a.errors['title'].any?
=> false
DataMapper
>> a = User.new
=> #<User @id=nil @login=nil @encrypted_password=nil>
>> a.errors['login']
=> nil
>> a.errors['login'].any?
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.any?
from (irb):10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment