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 |
Notes |
Start time |
<%= 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}”