Question

I'm making a simple to-do list application to teach myself Ruby on Rails, however I've run into a problem. I have a simple form that lists to-do items with a check box to the left of them, and an "Update" button at the bottom, as so:

[ ] Do the dishes
[ ] Take out the garbage
[ ] Take over the world
( Update )

Each to-do item is a separate record in the database with a "completed" boolean field. I want the form to submit a list of checked-off item ids to an action in which I can set each item's "completed" field to true, which will hide them from the view.

I know how to make a form that references multiple models, but not one that references multiple records of the same model. Any tips?

Thanks!

Was it helpful?

Solution

Railscasts is your friend!

http://railscasts.com/episodes/52-update-through-checkboxes

It's really simple:

# 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

# views\tasks\complete.html.erb
<% 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 %>

OTHER TIPS

Wow. That is some timing. The item right before this question in my RSS reader is the latest railscast from Ryan Bates and it appears to cover exactly what you are asking.

Why not use a remote function to update the database when an item is checked, and get rid of the update button altogether?

Since you can include the ID for each record when you create your form you will want the controller to iterate through items in you post then find and update each of those records accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top