Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

form_for builder not passed to fields_for in nested forms

#2023

In Rails 2.3, the nested forms support does not pass the builder to any nested fields_for calls. For example, if you say:

<% form_for(@forum_topic, :builder => MyFormBuilder) do |f| %>
  <%= f.error_messages %>
  <%= f.text_field :title %>
  <% f.fields_for :forum_post do |p| %>
    <%= p.text_area :body %>
  <% end %>
  <%= f.check_box :sticky %>
  <%= f.check_box :locked %>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

The custom "MyFormBuilder" builder does not get passed into the fields_for call explicitly:

<% form_for(@forum_topic, :builder => MyFormBuilder) do |f| %>
  <%= f.error_messages %>
  <%= f.text_field :title %>
  <% f.fields_for(:forum_post, :builder => MyFormBuilder) do |p| %>
    <%= p.text_area :body %>
  <% end %>
  <%= f.check_box :sticky %>
  <%= f.check_box :locked %>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

It seems like form_for should be able to store the :builder option in some type of instance variable and then pass it into fields_for blocks.

I can spin a patch, but would like an opinion on how to proceed. Looks like the FormBuilder version of fields_for would need to reference @options and pull apart *args* in vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb

Reported by Nate Wiger · February 19th, 2009 @ 08:56 PM

State: committed
Milestone: none
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Nate Wiger
    Nate Wiger
    • Tag set to 2.3, edge, fields_for, form_for, form_helper, patch

    Ok, easiest patch ever. Against trunk.

    February 19th, 2009 @ 09:03 PM

  2. Eloy Duran
    Eloy Duran
    • Assigned user set to Eloy Duran

    February 20th, 2009 @ 08:32 AM

  3. Eloy Duran
    Eloy Duran

    Please also add test coverage, for the case that args.last[:builder] was nil and was already set. Also, does this already work this way with the regular fields_for?

    February 20th, 2009 @ 08:58 PM

  4. Michael Koziarski
  5. Eloy Duran
    Eloy Duran

    Fixed.

    Note: In the doc part of the patch I thought only the top part of the file's documentation was over 80 columns wide, so I changed it to max. 80 cols. Then I realized that the rest of the file had the same problem. I'll have a talk with Pratik about standardizing this.

    February 26th, 2009 @ 11:41 AM

  6. Eloy Duran
    Eloy Duran
    • Assigned user changed from Eloy Duran to Michael Koziarski

    February 26th, 2009 @ 12:47 PM

  7. Repository
    Repository

    (from [3d1d422b8be027417cb640416d154171e75f03d1]) Pass a custom form builder on to nested fields_for calls. [#2023 form_for builder not passed to fields_for in nested forms status:committed]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    February 27th, 2009 @ 12:57 PM

  8. DHH
    DHH
    • State changed from new to committed

    February 27th, 2009 @ 12:58 PM