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!
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With