Skip to content

Instantly share code, notes, and snippets.

@javierm
Created July 29, 2010 16:49
Show Gist options
  • Save javierm/498632 to your computer and use it in GitHub Desktop.
Save javierm/498632 to your computer and use it in GitHub Desktop.
diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb
index 92c10b5..f540eaf 100644
--- a/lib/simple_form/components/errors.rb
+++ b/lib/simple_form/components/errors.rb
@@ -32,7 +32,11 @@ module SimpleForm
end
def errors_on_attribute
- object.errors[attribute_name]
+ if object.respond_to?(:errors)
+ object.errors[attribute_name]
+ else
+ []
+ end
end
def errors_on_association
diff --git a/test/components/error_test.rb b/test/components/error_test.rb
index 11a7ff5..3a165fc 100644
--- a/test/components/error_test.rb
+++ b/test/components/error_test.rb
@@ -23,6 +23,13 @@ class ErrorTest < ActionView::TestCase
assert_no_select 'span.error'
end
+ test "error should not generate messages when object doesn't respond to errors method" do
+ @user.instance_eval {undef errors}
+ with_error_for @user, :name, :string
+ assert_no_select 'span.error'
+ end
+
+
test 'error should generate messages for attribute with single error' do
with_error_for @user, :name, :string
assert_select 'span.error', "can't be blank"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment