Forms containing checkboxes and collections/object lists

Hi,

I’m really struggling to understand how to get checkboxes to work on a
list of objects. Essentially I have this:

  • An Issue model which has_many articles
  • An Article model which belongs to issue

So, I am pulling up a list of articles, all that are of type ‘ready’ and
either have an issue_id of 0 or article.issue_id == issue.id

I want to have a check box after each in the table. It will be checked
if you wish add the article to the current issue and unchecked if you
wish to set the issue_id for the article to 0 and not include it.

How in the blue blazes of hell do I do this with action view? The
‘form_for’ helper only seems to refer to on object not a collection.

On page 498 of ‘Agile Web D. with Rails’ there’s a section
about ‘Forms Containing Collections’ which implies you can use a text
field easily enough and this code works, adding an id into each text
field’s ID. However, doing the same thing with ‘check_box_tag’ fails
brutally (maybe I don’t understand this tag).

So I tried with ‘check_box’ but all this does is give me an error like
this:

undefined method `delete’ for 1:Fixnum

referencing the ‘check_box’ tag. I don’t have ‘delete’ anywhere in my
code.

I have tried to look over this:
http://wiki.rubyonrails.org/rails/pages/CheckboxHABTM but there is
querying of the database in the rhtml code, which seems wrong and not
something I’m entirely sure I can easily achieve.

Can someone give me a clear and easy way to do this sort of checkbox
use? Surely this has got to be one of the most basic uses of a checkbox
in code yet it seems hard to find something about?

Any help will be appreciated, even if it’s just a ‘DURR!’ and a link to
a very clear code example.

Thanks muchly,
Theo

Check out this railscast.

On Nov 11, 4:39 pm, Theo Graham-brown <rails-mailing-l…@andreas-

Chris B. wrote:

Check out this railscast.

#17 HABTM Checkboxes - RailsCasts

On Nov 11, 4:39 pm, Theo Graham-brown <rails-mailing-l…@andreas-

The problem is I’m not using HABTM. There will only ever be one article
in any given Issue, though an issue will have many articles.

THis doesn’t seem to work for my requirements.

Chris B. wrote:

Check out this railscast.

#17 HABTM Checkboxes - RailsCasts

On Nov 11, 4:39 pm, Theo Graham-brown <rails-mailing-l…@andreas-

Apologies. I think it will work!

Thanks a lot. The webcast is much easier to follow than the Wiki entry
on a similar subject.

I maybe be back, though. :frowning:

Thanks for your patience.