# AJAX - update item forms problem

**URL:** <https://www.ruby-forum.com/t/ajax-update-item-forms-problem/58171>\
**Category:** Rails\
**Created:** [April 19, 2006, 8:39am UTC](https://www.ruby-forum.com/t/ajax-update-item-forms-problem/58171 "2006-04-19T08:39:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [April 19, 2006, 8:39am UTC](https://www.ruby-forum.com/t/ajax-update-item-forms-problem/58171/1 "2006-04-19T08:39:05Z")

</div>

I have a table showing some records from the database. These records  
should be always ordered by some datetime field. The list of items is  
changed by AJAX using RJS scripts and edits are done by showing and  
hiding an update form under a chosen row. Adding and updating rows works  
great but my problem is when the order of items is changed after making  
some AJAX datetime updates update forms of these changed items are not  
filled with proper item data anymore when the form is shown. It seems to  
me the initial order of forms is remembered somewhere and doesn’t  
refresh.

Obviously I’m doing sth wrong here.  
Any help is appreciated. Here is how I do it.

## list.rhtml:

```
<div id="workout_error_list_<%= workout.id %>">
    <%= error_messages_for 'workout' %>
</div>

```

\<%= form\_remote\_tag :url =\> { :action =\> ‘update’, :id =\> workout },  
:html =\> { :id =\> “workout\_form\_#{workout.id.to\_s}” } %\>  
\<% @workout = workout %\>  
\<%= render :partial =\> ‘update\_form’ %\>  
\<%= end\_form\_tag %\>

```
</td>

```

## workouts\_controller.rb

def show\_workout\_update\_form  
@workout\_id = params[:id]  
end

def hide\_workout\_update\_form  
@workout\_id = params[:id]  
end

def update  
@workout = Workout.find(params[:id])  
@workout.exercises\_count = @workout.exercises.count  
@saved = @workout.update\_attributes(params[:workout])

```
return if request.xhr? # render RJS instead

```

## update.rjs

if @saved  
page.hide “workout\_edit\_#{@workout.id.to\_s}”  
flash[:notice] = “Workout updated.”  
session[:workout\_yellow] = @workout.id  
page.call ‘location.reload’  
else  
page.hide “workout\_edit\_#{@workout.id.to\_s}”  
page.replace\_html “workout\_error\_list\_#{@workout.id.to\_s}”,  
error\_messages\_for(‘workout’)  
page.replace\_html “workout\_form\_#{@workout.id.to\_s}”, :partial =\>  
‘update\_form’  
page.visual\_effect :appear, “workout\_edit\_#{@workout.id.to\_s}”,  
:duration =\> 0.5  
end

## \_update\_form.rhtml

**Edit workout**

* * *

| 

Name  
\<%= text\_field 'workout', 'name' %\>

 | 

Notes  
\<%= text\_area 'workout', 'description', :rows =\> 3 %\>

 | 

Start time  
\<%= datetime\_select 'workout', 'started\_at', :start\_year =\> 1980, :end\_year =\> 2010 %\>

 |

  
 \<%= submit\_tag "Update", :class =\> 'button' %\>

or   
\<%= link\_to\_remote ‘Cancel’, :url =\> { :action =\>  
‘hide\_workout\_update\_form’, :id =\> @workout } %\>

## show\_workout\_update\_form.rjs

page.visual\_effect :appear, “workout\_edit\_#{@workout\_id.to\_s}”,  
:duration =\> 0.5

## hide\_workout\_update\_form.rjs

page.hide “workout\_edit\_#{@workout\_id.to\_s}”

---

<div class="post-metadata">

**Author:** ![mike343](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike343](https://www.ruby-forum.com/u/mike343)\
**Post date:** [April 19, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/ajax-update-item-forms-problem/58171/2 "2006-04-19T13:38:39Z")

</div>

Fixed already by this:

def show\_workout\_update\_form  
@workout\_id = params[:id]  
@workout = Workout.find(@workout\_id)  
render :update do |page|

```
  page.replace_html "workout_form_#{@workout_id.to_s}", :partial => 

```

‘update\_form’, :object =\> @workout  
page.visual\_effect :appear, “workout\_form\_#{@workout\_id.to\_s}”,  
:duration =\> 0.5  
end  
end
