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

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:

On Jun 17, 7:15 pm, Bob S. [email protected]
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:

Learn about controllers and actions in Rails. I would recommend the
Agile Web D. with Rails or Ruby for Rails as good books to
start with.

Eventually you’ll learn about the link_to helper you can use in your
views, for example:

<% link_to ‘Complete This To-Do Item’, :action => ‘completed’ %>

and you have to implement the corresponding ‘completed’ action in your
controller. But that’s a lot to explain here, so start with one of
the books above.

Jeff

Bob S. wrote:

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

http://autopendium.com
Stuff about old cars