Forum: Ruby on Rails AJAX - update item forms problem

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.
Michael M. (Guest)
on 2006-04-19 10:39
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:
-----------
<tr id="workout_edit_<%= workout.id %>" style="display:none;">
    <td colspan="7" style="background-color: #eee; padding: 10;">
        <div id="workout_error_<%= workout.id %>">

    <div id="workout_error_list_<%= workout.id %>">
        <%= error_messages_for 'workout' %>
    </div>
</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>
</tr>

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
------------------
<!--[form:workout]-->
<strong>Edit workout</strong>
<hr/>

<table>
<tr>


<td>
<p><label for="workout_name">Name</label><br/>
<%= text_field 'workout', 'name'  %></p>
</td>
<td>
<p><label for="workout_description">Notes</label><br/>
<%= text_area 'workout', 'description', :rows => 3  %></p>
</td>
<td>
<p><label for="workout_started_at">Start time</label><br/>
<%= datetime_select 'workout', 'started_at', :start_year => 1980,
:end_year => 2010  %></p>
</td>
</tr>
</table><br/>
   <%= submit_tag "Update", :class => 'button' %>

  or <span class="red">
    <%= link_to_remote 'Cancel', :url => { :action =>
'hide_workout_update_form', :id => @workout } %>
</span>
<br/>

<!--[eoform: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}"
Michael M. (Guest)
on 2006-04-19 15:38
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
This topic is locked and can not be replied to.