List of unchecked checkboxes

Hi all

Having kinda trouble with checkboxes. Let's assume "Order has_and_belongs_to_many :products" and vice versa. Now let's assume there are products with IDs 1, 2 and 3 in the db and I'm editing an existing Order with the following view:

<% form_for(:order) do |f| %>   <% f.check_box(:product_ids, 1) %>   <% f.check_box(:product_ids, 2) %>   <% f.check_box(:product_ids, 3) %> <% end %>

First let's set all checkboxes. The resulting params hash will be: param[:order][:product_ids] # => ['1', '2', '3']

This is perfect as the controller code will do as desired: @order.update_attributes(params[:order]) @order.product_ids # => [1,2,3]

But now the same with all checkboxes NOT set. The params hash: param[:order][:product_ids] # => nil

Way uncool, because this is what the controller does: @order.update_attributes(params[:order]) @order.product_ids # => [1,2,3]

It *would* be cool though to have an empty array instead of nil in the params hash there as this would correctly remove all referenced products from this order.

Now I could of course add this to the controller: @order.product_ids = if @order.product_ids.nil?

But as my view code is generated by a FormHelper plugin, I'd like to make it work without the need to touch the controller code.

Can I add anything to the view that will ensure the params hash will return rather than nil if no checkbox is set?

Thanks for your help, -sven

You could do this before calling update_attributes:

param[:order][:product_ids] ||=

This will initialize the product_ids parameter to an empty array if it is nil.

If you want to make this the default behavior, you'll need to open up some Rails code. Not sure exactly where you'd make a change, but it should be possible in just a few lines of code.

<% form_for(:order) do |f| %>   <% f.check_box(:product_ids, 1) %>   <% f.check_box(:product_ids, 2) %>   <% f.check_box(:product_ids, 3) %> <% end %>

This is wrong anyway, should be:

<% form_for(:order) do |f| %>   <% f.check_box('product_ids', 1) %>   <% f.check_box('product_ids', 2) %>   <% f.check_box('product_ids', 3) %> <% end %>

I found a solution myself :slight_smile: Seems to work at least with the UrlParser in Rails 2.0.2.

<% form_for(:order) do |f| %>   <% f.hidden_input('product_ids', '') %>   <% f.check_box('product_ids', 1) %>   <% f.check_box('product_ids', 2) %>   <% f.check_box('product_ids', 3) %> <% end %>

The empty value is skipped when the IDs are compiled, but it enforces an in the params hash if nonoe of the checkboxes are selected.

I have a similar problem, but but your solution doesn't seem to work for me.

I have something like this: <% form_for...

<%for category in @categories do%>     <% f.check_box('category_ids',category.id)%> <%end%>

<%end%>

But I get a "undefined method `merge' for 1:Fixnum"

on the check_box line.

Any ideas on whats different? If I hard code the id as you have I get the same result.

Thanks, j

<%for category in @categories do%>     <% f.check_box('category_ids',category.id)%> <%end%>

I'm using check_box_tag (for contextual reasons, it's in a plugin), but it looks to me as if you use the check_box helper incorrectly.

According to the doc at railsapi.org - railsapi Resources and Information. it should be as follows:

f.check_box(method, options = {}, checked_value = "1", unchecked_value = "0")

So you pass a number (category.id) where a hash (options) is expected.

Thanks, but I can't seem to make that version of the fuction work either. That's ok though I'll look elswhere to find a working example of what I'm trying to do, just thought this was it.

Thanks, j