Rahul
Rahul

Reputation: 464

Update database attribute from Javascript

In my ruby on rails app, I am trying to allow a user to change a field on the screen and as a result, update the database.

A user is promoted with the following screen:

<td id="sb_user-<%=server.id%>" ondblclick="changeUser(<%= server.id %>);"><%= server.sb_user %></td>

enter image description here

When they double click on the sb_user field, a javascript function is called and changes the field to a textbox with a simple go button:

    <script type="text/javascript">
        function changeUser(id) {
            //Stop the auto refresh until user hits enter
            clearInterval(refreshInterval);
            $("#sb_user-"+id).html("<form name='user'><div class='input-append'><input name='user_input' class='span1' id='appendedInputButton' size='16' type='text'/><button class='btn' type='button'>Go!</button></form></div>"); 
        }
    </script>

enter image description here

When a user enters text into the textbox and clicks go, I need it so that the database field server.sb_user is updated to whatever the user entered in that field.

Anyone have suggestions on how to do this?

Upvotes: 0

Views: 214

Answers (1)

davidb
davidb

Reputation: 8954

What you are searching for is in place editing. Theres a railscast for that:

http://railscasts.com/episodes/302-in-place-editing

Upvotes: 3

Related Questions