Click On a Link to Update Database Record. How Do You Do It?

Bob Sanders wrote:

I'm experimenting with creating a very small To-Do List.

I want to be able to click on a link that says: "Complete This To-Do Item" right beside the item -- and then the database field underneath, "Status", will update from "Open" to "Completed".

How would you do it?

(Also, if there's a better way to do this, I'm open ears :slight_smile:

My recommendation would be to start with a non-Ajax version, and then add the Ajax behaviour in unobtrusively afterwards (something like LowPro or UJS). I would recommend that the (GET) link goes to a confirmation page, which then POSTs the update (destructive GET requests are not a good idea). Then add the Ajax functionality on top of that, to submit the POST directly from the link. That way you have nicely degrading clean code.

Hope this makes sense. I do something exactly like this on Autopendium, with the following code apply_behaviour 'input.todo_complete_box', make_observed(:field, {:url => {:controller => 'todos', :action => 'update', :id => nil}, :method => "put", :with => "'id=' + element.id.replace('todo_complete_box_','') + '&todo[completed]=' + (element.checked ? 1 : 0) "})

The apply_behavour is from the UJS plugin, and you actually tick or un-tick checkboxes (which only show if JS is enabled).

Hope this helps, Chris