Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fields_for does not accept record object with options #13124

Closed
bughit opened this Issue · 5 comments

4 participants

@bughit

as the docs example indicates they should:
http://guides.rubyonrails.org/form_helpers.html#using-form-helpers

        def instantiate_builder(record_name, record_object, options)
          case record_name
          when String, Symbol
            object = record_object
            object_name = record_name
          else
            object = record_name
            object_name = model_name_from_record_or_class(object).param_key
          end

          builder = options[:builder] || default_form_builder
          builder.new(object_name, object, self, options)
        end

possible fix:

        def instantiate_builder(record_name, record_object, options)
          case record_name
          when String, Symbol
            object = record_object
            object_name = record_name
          else
            object = record_name
            options = record_object if record_object.is_a?(Hash)
            object_name = model_name_from_record_or_class(object).param_key
          end

          builder = options[:builder] || default_form_builder
          builder.new(object_name, object, self, options)
        end
@al2o3cr

Can you provide an example of how this looks in user-level code (ie, actually calling fields_for)?

@bughit

there's an example in the link:

<%= form_for @person do |person_form| %>
  <%= person_form.text_field :name %>
  <% @person.addresses.each do |address| %>
    <%= person_form.fields_for address, index: address do |address_form|%>
      <%= address_form.text_field :city %>
    <% end %>
  <% end %>
<% end %>
@al2o3cr

The code for the form helpers is somewhat confusing - there are TWO definitions of fields_for, each with slightly different behavior. The one used above is defined on ActionView::Helpers::FormBuilder here and does the necessary shuffling of record_name, record_object, and fields_options.

There is another definition here which is not invoked on a FormBuilder object. Calling this one would require code like this:

<%= fields_for @person, include_id: false do |person_form| %>
...
<% end %>

It does appear that in this case, the hash of options will be passed into instantiate_builder in the record_object parameter and then subsequently ignored.

Some digging in the issues bin turned up #568, which looks highly related. In particular, it appears that the rewrite in #568 dropped the extract_options! call that would have correctly handled the options in the second argument.

Proposed resolution: add the test case described in the comments to #568 and fix the behavior of the non-FormBuilder version. However, as discussed there it's not clear whether the change should be in fields_for or in instantiate_builder: my guess is that it should live in fields_for, since that method's signature is responsible for the default values.

/cc @carlosantoniodasilva

@bughit bughit added the stale label
@rafaelfranca
Owner

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot
Collaborator

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.