#52
Jul 02, 2007

Update through Checkboxes

See how to select multiple items using checkboxes and perform an action on the selected items in this episode.
Download (27 MB, 10:02)
alternative download for iPod & Apple TV (14.6 MB, 10:02)

Resources

# routes.rb
map.resources :tasks, :collection => { :complete => :put }

# tasks_controller.rb
def complete
  Task.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
end
<% form_tag complete_tasks_path, :method => :put do %>
  <ul>
  <% for task in @incomplete_tasks %>
    <li>
      <%= check_box_tag "task_ids[]", task.id %>
      <%= task.name %>
    </li>
  <% end %>
  </ul>
  <%= submit_tag "Mark as Complete" %>
<% end %>

14 comments

chineseGuy Jul 02, 2007 at 00:05

let me see


Leeky Jul 02, 2007 at 00:38

Another great railscast as usual. One thing that I'd like to know is if there's a way to make the checkboxes each have a different ID in the HTML code, and still have the form work?

I noticed in the example that every checkbox has the same ID, which is invalid, as ID's are meant to be unique within a XHTML document.


Sintaxi Jul 02, 2007 at 04:26

Thanks again Ryan for you great work.

@Leeky - It is actually the 'name' of the field that is referenced (and not the id) for submission. It is common practice (as many rails helpers do) to call the id and name the same. Im sure you can used the name as described in this screencast and use the dom_id plugin (http://topfunky.net/svn/plugins/dom_id/) to give your forms unique ids. This would be helpful for validation and for giving this task ajaxy goodness.


JackVandaL Jul 02, 2007 at 07:56

Great job Ryan. One thing your code snippet has "form_for" not "form_tag".


Rob Jul 02, 2007 at 08:18

If we were to do this with multiple users, how can we first ensure that the task IDs belong to the current logged in user (in case someone edits the HTML and puts in a task ID that doesn't belong to them)?


Paul Barry Jul 02, 2007 at 11:16

@Rob

current_user.tasks.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
end


JackVandaL Jul 02, 2007 at 12:40

Sorry two other things:
- submit_tag needs a closing %>
- it's ":collection" not ":collections"
:)


Ryan Bates Jul 02, 2007 at 15:41

@JackVandaL, thanks, fixed. I gotta learn to copy and paste the code instead of retyping it. :/


Manu Jul 03, 2007 at 10:47

Ryan,

Great episode. I have question concerning the controller. I see the pattern of always having a new -> create, edit -> update method.. But then I see some repeated code in the new and the create (like typically find the object by ID). Could use call the new method to do the setup of the default data and override them from the create method? Or is there a better way to refactor that?


Mark Jan 18, 2008 at 05:23

This was great. Just wondering if there is a way to do this without using REST?

My app is not as clean as this one at this point and I am lost when it comes to getting the "collection" of data passed to the controller method.


Frode Marton Meling Mar 12, 2008 at 02:44

I have a question. In this example the items disappear from the list when they are marked as complete.
What if you want to display the complete list all the time, but the check_box default value is based on the values from the tasks.


kino May 23, 2008 at 01:54

By immersing ourselves meditatively in the general intentions of modes of consciousness, we discover that noematic descriptions are "in themselves".


skoppy Jun 09, 2008 at 09:54

I am still a little bit confused on how I can give each of the checkboxes a unique id attribute. I'd like to create a label for each of my checkboxes which I am currently unable to do without unique ids.

Any help will be most appreciated.


Brandan Lennox Jun 30, 2008 at 14:17

@skoppy

If you are using fields_for in the way that Ryan is in his code, Rails should provide a unique id. If that's not working, you could always pass your own id as an HTML attribute:

f.check_box :property, :id => "some-unique-id"

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player