Topic: Checkboxes in Todo list application

Hi guys,

I'm getting into RoR quite a bit now and enjoying it for the most part smile. I've started off making a todo List with the adding and editing of items. I am however having a little trouble with checkboxes. I have a field called completed in my database which can either be 0 (Not completed) or 1 (completed) which works well with checkboxes as I understand because 0 will give an unchecked box and 1 a checked box. So this is my _form.rhtml file in the view folder: http://pastie.caboo.se/42290 . This works because I can check the box and omit the changes in the edit view and this will change the record in the database. It also mirrors the changes when I go to the 'Show' page of the particular item, however it is not mirrored in the list view with the correct checkbox. Any ideas why? Here is the code in list.rhtml: http://pastie.caboo.se/42291

Rails is recognising it when it has been set to completed as you can see in this screenshot (list): http://www.mholland.org/list.png . But not displaying the right checkbox. Here in the edit view however, it is displaying the right checkbox. http://www.mholland.org/edit.png .

Any ideas/help/responses are greatly appreciated.
Thanks very much
Matt.

Matt
| My blog smile |

Re: Checkboxes in Todo list application

Form helpers (like check_box) look for an instance variable that is the same name as the model. If you set the instance variable in the loop it should work:

<% for @todo in @todos_completed %>
  <%= check_box :todo, :completed %>
  #...
<% end %>

At that point, however, clicking the box will not do anything as far as updating the model.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Thanks for the quick response Ryan, unfortunately that didn't work sad Any other ideas?

Matt
| My blog smile |

Re: Checkboxes in Todo list application

You can try check_box_tag:

<% for todo in @todos_completed %>
  <%= check_box_tag :completed, "1", todo.completed? %>
  #...
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Thanks it worked! Just so I learn for future occurences what each part of the code is for? The part you added I mean.

Thanks so much for your time
Matt smile

Matt
| My blog smile |

Re: Checkboxes in Todo list application

The check_box method that you were using before is designed to edit a model's attribute. The check_box_tag helper that I am using is generic - it can be used for anything. Ideally check_box should work, I'm not sure why it isn't.

As you can see in the documentation, I'm just supplying a name for the checkbox, passing the checked value which is "1" and passing a boolean specifying if it should be checked or not. The "completed?" method is something Rails generates automatically for you and returns true/false depending on the value in the column.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

That seems reasonable, thanks for all your help big_smile, I should be doing a lot more Ruby now, so no doubt you'll be seeing a bit more of me tongue Could you push me towards the general idea of how to edit whether the item is completed or not using the checkbox in the list view by any chance?

Thanks

Last edited by Matt (2007-02-22 18:49:03)

Matt
| My blog smile |

Re: Checkboxes in Todo list application

It depends how you want to do it. Do you want to use AJAX so it automatically sets the value upon complete? If so, check out this post.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

I'd prefer doing it will just a form button rather than jumping straight to AJAX.

Thanks for all this help btw smile

Matt
| My blog smile |

Re: Checkboxes in Todo list application

Do you want a form button for every item? In other words, each item is a separate form? If you just want one "submit" button to update all of the checkboxes that may be harder than the AJAX approach.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Sure, would you mind giving me the general idea of how to make each item a separate form?

Matt
| My blog smile |

Re: Checkboxes in Todo list application

You can try using form_for, like this:

<% for todo in @todos_completed %>
  <% form_for :todo, todo, :url => { :action => :update } do |f| %>
    <%= f.check_box :completed %>
    #...
    <%= submit_tag 'Update' %>
  <% end %>
<% end %>

Here we are going back to check_box instead of check_box_tag. Hopefully with form_for the value will be set properly.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Couldn't find Todo without an ID

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1012:in `find_from_ids'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:419:in `find'
app/controllers/todo_controller.rb:40:in `update

When I go to the list view again though, the checkbox state will have changed correctly but it wont have changed in the database and stays in the same Todos completed section.

Last edited by Matt (2007-02-23 14:10:30)

Matt
| My blog smile |

Re: Checkboxes in Todo list application

I forgot the id in the URL (I've been doing that lately).

<% form_for :todo, todo, :url => { :action => :update, :id => todo } do |f| %>

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Absolutely awesome dude, thanks so much for your help smile

Matt
| My blog smile |

Re: Checkboxes in Todo list application

No problem. Glad it's working.

Railscasts - Free Ruby on Rails Screencasts

Re: Checkboxes in Todo list application

Just another quick question, you must be sick of me XD, what is the do |f| for?

Matt
| My blog smile |

Re: Checkboxes in Todo list application

The form_for method accepts a block, and the "f" is a parameter for that block. The f variable just contains a form builder object which aids in creating the form fields (which is why we call "f.check_box" instead of just "check_box").

Railscasts - Free Ruby on Rails Screencasts