Forum: Ruby on Rails Click On a Link to Update Database Record. How Do You Do It?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c2f3ce7125411ff78a1d3e902aa2a4d?d=identicon&s=25 Bob Sanders (adistarmid)
on 2007-06-18 02:15
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 :)
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-06-18 03:09
(Received via mailing list)
On Jun 17, 7:15 pm, Bob Sanders <rails-mailing-l...@andreas-s.net>
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 Development 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
softiesonrails.com
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris Taggart (christ)
on 2007-06-26 12:00
(Received via mailing list)
Bob Sanders 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
This topic is locked and can not be replied to.