Forum: Ruby on Rails Checkbox Help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bing (Guest)
on 2006-05-31 14:17
Hi guys,

I am need to help again. Sorry for the newbie query but would definitely
appreciate the help.

Here's the q: I generate a list of items from the database and render
them out with a checkbox beside them. A user may click more than one
item and I would want to save all checked items into another table. I
have been able to give each checkbox a unique id . Would anyone be able
share some code example? What I'm not familiar with is how the values
are passed on by the form so that the def can handle it?

Thanks,

Bing
Mathias W. (Guest)
on 2006-05-31 17:07
(Received via mailing list)
On 5/31/06, Bing <removed_email_address@domain.invalid> wrote:
> are passed on by the form so that the def can handle it?
>
> Thanks,
>
> Bing

Depending on what your models look like you may be able to do this
fairly easy and have Rails do it automatically for you. But for a
basic version, try just doing a
<%= debug params %>
in your view, that's where your data from the checkboxes appear. So
you should find the data in params[:formname][:checkboxid]

Hope that helps you get going in the right direction. :) I have a very
similar thing I did myself just a few days ago, but I don't have the
code for it here, I could pass it along later today if you want.

Mathias.
Ole Christian Rynning (Guest)
on 2006-06-01 17:31
(Received via mailing list)
Try something like:

view:
<%= form_tag(:action => "item_check") %>
  .. for each item ...
  <%= check_box ("to_check", order.id, {}, "yes", "no") %>
  ...
<%= submit_tag %>
<%= end_form_tag %>

controller:
def item_check
  if things_to_check = params[:to_check]
    things_to_check.each do |item_id, act|
      if act == "yes"
        ...
      else
        ...
      end
    end
  end
  @items = Item.unchecked
end


Bing wrote:
> are passed on by the form so that the def can handle it?
>
> Thanks,
>
> Bing
>
>


--
A | c/o Marie Eikrem, Fagerborggt 48A, NO-0360 OSLO
M | +47 411 44 384
W | http://www.foo.mu
This topic is locked and can not be replied to.