Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

objectify_options on custom form builders #10115

Closed
mdedetrich opened this Issue Apr 6, 2013 · 1 comment

Comments

Projects
None yet
2 participants

When creating a custom form builder by subclassing ActionView::Helpers::FormBuilder, objectify_options is not called on any of the option parameters in any methods defined unless the user specifically knows about this private method

For example, my own simple custom form builder used to look like this.

def text_field_dollar(attribute,html_options={})
      @template.content_tag(:div,
        @template.text_field(@object_name,attribute,html_options) +
        @template.content_tag(:div,"$",'style' =>'display:inline-block;margin-right:3px;vertical-align:middle;margin-top:4px;float:left;')
      )
end

The problem is that since objectify_options was not not called on the html_options variable, the "value" field (which when the form is called in an update action inside the controller would contain the value of the attribute in the model by default) wasn't being filled. To fix this, a user has to explicitly call objectify_options on the options variable

This workflow is both very DRY and obscure/confusing. objectify_options is only documented here (http://apidock.com/rails/ActionView/Helpers/FormBuilder/objectify_options) with no comments and isn't mentioned anywhere else. Using the above as an example, one would look here

def text_field(object_name, method, options = {})
, and in that source there is no mention of using objectify_options, nor is it being called.

tl;dr It is not obvious (even in the slightest) that a user has to use objectify_options on the options parameter to mimic the default form builders methods when creating their own form builder methods.

Either the documentation needs to make this very clear, or this behavior should be automatic when using custom form builders by subclassing ActionView::Helpers::FormBuilder

wangjohn added a commit to wangjohn/rails that referenced this issue Apr 7, 2013

Added documentation to the FormBuilder class, should help
clarify issue #10115. Also made the field_helpers an explicit list of
methods.
Contributor

wangjohn commented Apr 7, 2013

I've added a lot of documentation in PR #10122 which should make it more clear what's going on in the FormBuilder class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment