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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> <% 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 %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I found a solution myself :-) 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jan 18, 1:53 am, Sven Schwyn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I found a solution myself :-) 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> <%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 http://www.railsapi.org/check_box 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jan 25, 1:55 am, Sven Schwyn <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > <%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 athttp://www.railsapi.org/check_boxit 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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---