-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Models without error
hash generate undefined method '[]' for nil:NilClass
#24
Comments
Was thinking about this a bit. What you're describing is a bit odd. If you're not actually sending ActiveRecord object you should use class FormObject
include ActiveModel::Model
end And then your form renders as if it's dealing with normal ActiveRecord object. Now, if you want to render form and pass an object that that only holds accessors and nothing else you'd need to sense if object responds to |
Yeah, this is a bit confusing because I had a specific use-case in mind when I ran into this issue and perhaps didn't describe it adequately. The purpose of the changes here were to attempt to replicate the experience you receive from the vanilla The particular scenario I was looking to resolve revolves around nested hashes in ActiveRecord models (eg. JSONB fields in Postgres models) whereby setting these properties on a form can be done by creating an object with the appropriate accessors and passing that into a Based on what I saw in the Vanilla forms source the only hard requirement for the object passed into the form is that it has the setter/getter methods. If we harden up the error method check to fix the case where error isn't defined would that be an appropriate solution in your mind? |
Valid form elements generate
undefined method '[]' for nil:NilClass
when the form's model does not include anerrors
hash.There are some scenarios when rails forms models are not set from an ActiveRecord model. In these scenarios, unless they emulate the
errors
hash produced by ActiveRecord, the rendering of the form element will result in the aforementioned error.Example:
In this scenario, with vanilla rails form elements, OpenStruct provides all the necessary methods for the
nested_hash_field
property to be rendered correctly. Usingcomfy-bootstrap-form
this will generate an error unless thenested_hash
includes anerrors
property.Suggested Fix
The
draw_errors
method inform_builder.rb
already includes a fast-return path if the model isnil
. The criteria should this should be expanded to also return if theerrors
hash is not present.The text was updated successfully, but these errors were encountered: