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

**URL:** <https://www.ruby-forum.com/t/click-on-a-link-to-update-database-record-how-do-you-do-it/107090>\
**Category:** Rails\
**Created:** [June 18, 2007, 2:15am UTC](https://www.ruby-forum.com/t/click-on-a-link-to-update-database-record-how-do-you-do-it/107090 "2007-06-18T02:15:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 18, 2007, 2:15am UTC](https://www.ruby-forum.com/t/click-on-a-link-to-update-database-record-how-do-you-do-it/107090/1 "2007-06-18T02:15:39Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 18, 2007, 3:09am UTC](https://www.ruby-forum.com/t/click-on-a-link-to-update-database-record-how-do-you-do-it/107090/2 "2007-06-18T03:09:38Z")

</div>

On Jun 17, 7:15 pm, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 🙂

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

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 26, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/click-on-a-link-to-update-database-record-how-do-you-do-it/107090/3 "2007-06-26T12:00:05Z")

</div>

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](http://autopendium.com)  
Stuff about old cars
