Handling multiple Check boxes in a XHTML Strict way

Hi,

The default way we handle an array of checkboxes is like this:

<% items.each do |item| %>
  <%= check_box_tag 'item[]', "#{item.id}" %>
<% end -%>

this returns an array item[] that holds the ids of all the items.

Now the real issue:

1. The above loop will same ids to 'n' items
2. All ids will be 'item[]'

Both of the above points are not XHTML Strict 1.0 valid and hence I am
seeking a solution.

An alternative could be like I assign each check box a unique id like
this:

<% items.each do |item| %>
  <%= check_box_tag "item_#{item.id}", "#{item.id}" %>
<% end -%>

But this will involve an overhead of collecting all ids in controller.

Any new ideas ??

~rd

The default way we handle an array of checkboxes is like this:

<% items.each do |item| %>
  <%= check_box_tag 'item[]', "#{item.id}" %>
<% end -%>

this returns an array item[] that holds the ids of all the items.

Now the real issue:

1. The above loop will same ids to 'n' items
2. All ids will be 'item[]'

Both of the above points are not XHTML Strict 1.0 valid and hence I am
seeking a solution.

gsub to replace the square brackets with something else?

An alternative could be like I assign each check box a unique id like
this:

<% items.each do |item| %>
  <%= check_box_tag "item_#{item.id}", "#{item.id}" %>
<% end -%>

But this will involve an overhead of collecting all ids in controller.

You first example shows using item.id, so how is not also an overhead
there? If you need the ids then you need the ids, overhead or not.

The default way we handle an array of checkboxes is like this:

<% items.each do |item| %>
  <%= check_box_tag 'item[]', "#{item.id}" %>
<% end -%>

this returns an array item[] that holds the ids of all the items.

Now the real issue:

1. The above loop will same ids to 'n' items
2. All ids will be 'item[]'

Both of the above points are not XHTML Strict 1.0 valid and hence I am
seeking a solution.

An alternative could be like I assign each check box a unique id like
this:

<% items.each do |item| %>
  <%= check_box_tag "item_#{item.id}", "#{item.id}" %>
<% end -%>

But this will involve an overhead of collecting all ids in controller.

Any new ideas ??

Yes. Use the same name attribute with [] and different ids:

<% 5.times do |i| %>
<%= check_box_tag "cb_[#{i}]", value="#{i}", false, :id => "cb_#{i}" %>
<% end %>

Regards,
Rimantas

Thanks a lot ! I got it solved.

So stupid of me to not note this fact that rails works by collecting
'name' attribute and not 'id'.

regards,
~rd

Rimantas Liubertas wrote: