Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9883 (closed enhancement: fixed)

Opened 11 months ago

Last modified 7 months ago

[PATCH] Allow "index" option default on fields_for and form_for

Reported by: rmm5t Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: fields_for form_for verified
Cc:

Description

Ryan Bates' screencast #75 touches on an approach for handling multiple objects with one form. This approach yielded a snippet like the following:

<% fields_for "project[task_attributes][]", task do |f| %>
  <%= f.text_field :name, :index => nil %>
  <%= f.hidden_field :id, :index => nil %>
  <%= f.hidden_field :should_destroy, :index => nil %>
<% end %>

He mentioned that the :index => nil options were ugly, and I agree. I thought it would be nice to be able to pass the :index option to the fields_for helper instead. For example:

<% fields_for "project[task_attributes][]", task, :index => nil do |f| %>
  <%= f.text_field :name %>
  <%= f.hidden_field :id %>
  <%= f.hidden_field :should_destroy %>
<% end %>

This reduces the clutter and avoids the duplication. This patch adds support for this. It includes tests and some brief rdoc explaining the option.

Attachments

add_index_override_to_fields_for_helper.diff (6.0 kB) - added by rmm5t on 10/15/07 23:47:38.
Allow "index" option to be passed to fields_for helper. Tests and rdoc included.
add_index_override_to_fields_for_helper_2.diff (5.8 kB) - added by rmm5t on 12/04/07 16:38:39.
Rebased since it's been a while
add_index_override_to_fields_for_helper_3.diff (6.5 kB) - added by rmm5t on 12/30/07 13:08:27.
improved tests from the original and fixed doc typo

Change History

10/15/07 23:47:38 changed by rmm5t

  • attachment add_index_override_to_fields_for_helper.diff added.

Allow "index" option to be passed to fields_for helper. Tests and rdoc included.

10/15/07 23:54:47 changed by rmm5t

  • summary changed from [PATCH] Allow "index" option override on fields_for and form_for to [PATCH] Allow "index" option default on fields_for and form_for.

10/18/07 12:32:41 changed by pimpmaster

+1 vote. I have already applied the patch and am loving my multi-model forms!

10/25/07 15:17:34 changed by chmurph2

+1

12/04/07 03:54:58 changed by swindsor

+1 vote. The railscasts provides a very useful pattern to follow, and this makes the code much cleaner.

12/04/07 16:38:39 changed by rmm5t

  • attachment add_index_override_to_fields_for_helper_2.diff added.

Rebased since it's been a while

12/04/07 16:39:02 changed by rmm5t

  • keywords changed from fields_for form_for to fields_for form_for verified.

12/30/07 04:27:13 changed by andrewf

+ 1 vote. any updates on when this might be included?

12/30/07 13:08:27 changed by rmm5t

  • attachment add_index_override_to_fields_for_helper_3.diff added.

improved tests from the original and fixed doc typo

02/02/08 06:16:07 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8786]) Introduce the :index option for form_for and fields_for to simplify multi-model forms (see http://railscasts.com/episodes/75). Closes #9883.