Wish list: form helpers for collections of new_record?s

Based on this from AWDWR 2nd ed.:

“If you need to edit multiple objects from the same model on one form,
add
open and closed brackets to the name of the instance variable you pass
to
the form helpers. This tells Rails to include the object’s id as part of
the field
name. For example, the following template lets a user alter one or more
image
URLs associated with a list of products.”

Wouldn’t it be nice if you could do this kind of thing to non-saved
model objects (which don’t have ids yet) as well? Maybe the open/closed
brackets could indicate to include the index of the object in the
collection if the object were a new_record? and then you could more
easily update attributes on the objects in that collection.

As it stands, I have to set up and maintain a “name unique-ing” scheme
for my form fields and know about that scheme in a controller when I go
to save the object.

Make sense?

Thanks,
Wes

Wes G. wrote:

URLs associated with a list of products."

Wouldn’t it be nice if you could do this kind of thing to non-saved
model objects (which don’t have ids yet) as well? Maybe the open/closed
brackets could indicate to include the index of the object in the
collection if the object were a new_record? and then you could more
easily update attributes on the objects in that collection.

You can do this easily with the :index option. e.g.

<% @collection.each_with_index do |@item, i| %>
<%= text_field :item, :method1, :index => i %>
<%= text_field :item, :method2, :index => i %>
<% end %>

posts params

{:item => {1 => {:method1 => ‘a’, :method2 => ‘b’},
2 => {:method1 => ‘c’, :method2 => ‘d’}}}

You can also use square brackets to get automatic
indexing of form fields into param arrays:

<% @collection.each do |item| %>
<%= text_field_tag ‘item[method1][]’, item.method1 %>
<%= text_field_tag ‘item[method2][]’, item.method2 %>
<% end %>

posts params

{:item => {:method1 => [‘a’, ‘c’], :method2 => [‘b’, ‘d’]}}


We develop, watch us RoR, in numbers too big to ignore.

Mark Reginald J. wrote:

You can do this easily with the :index option. e.g.

<% @collection.each_with_index do |@item, i| %>
<%= text_field :item, :method1, :index => i %>
<%= text_field :item, :method2, :index => i %>
<% end %>

posts params

{:item => {1 => {:method1 => ‘a’, :method2 => ‘b’},
2 => {:method1 => ‘c’, :method2 => ‘d’}}}

You can also use square brackets to get automatic
indexing of form fields into param arrays:

<% @collection.each do |item| %>
<%= text_field_tag ‘item[method1][]’, item.method1 %>
<%= text_field_tag ‘item[method2][]’, item.method2 %>
<% end %>

posts params

{:item => {:method1 => [‘a’, ‘c’], :method2 => [‘b’, ‘d’]}}

Yep, this is basically the approach that I ended up with - what’s nice
is that it works regardless of whether or not the collection members are
actually saved.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs