I''ve got the following sortable list in one of my views: <ul id="sortable_list" class="sortable_list"> <%= render(:partial => ''section'', :collection => @sections) %> </ul> <div class="new_section" id="new_section"> <%= form_remote_tag(:url => {:action => ''edit_section'', :section => nil} %> Section Name: <%= text_field_tag ''section[name]'' %> <%= submit_tag(''Create new section'') %> <%= end_form_tag %> </div> I can dynamically update the contents of this list using the following: def edit_section if request.post? @section = Section.find_by_id(params[:section]) || Section.new flash.now[:notice] = "New section \"#{@section.name}\" successfully created." if @section.update_attributes(params[:section]) render :update do |page| page.replace_html(''sortable_list'', :partial => ''section'', :collection => Section.find_all) end end end however, once I update the list of sections, I can no longer re-order them (until I reload the page).. I''d like to know if it''s possible to dynamically update a sortable list by using replace_html, and still allow the list to be sorted, without manually reloading the page. Thanks, Mike
Mike Garey
2006-Nov-04 02:25 UTC
[Rails] Re: Dynamically updating a sortable list using RJS
On 9/20/06, Mike Garey <random52k@gmail.com> wrote:> I''ve got the following sortable list in one of my views: > > <ul id="sortable_list" class="sortable_list"> > <%= render(:partial => ''section'', :collection => @sections) %> > </ul> > > <div class="new_section" id="new_section"> > <%= form_remote_tag(:url => {:action => ''edit_section'', :section => nil} %> > Section Name: <%= text_field_tag ''section[name]'' %> > <%= submit_tag(''Create new section'') %> > <%= end_form_tag %> > </div> > > I can dynamically update the contents of this list using the following: > > def edit_section > if request.post? > @section = Section.find_by_id(params[:section]) || Section.new > > flash.now[:notice] = "New section \"#{@section.name}\" > successfully created." if @section.update_attributes(params[:section]) > > render :update do |page| > page.replace_html(''sortable_list'', :partial => ''section'', > :collection => Section.find_all) > end > end > end > > however, once I update the list of sections, I can no longer re-order > them (until I reload the page).. I''d like to know if it''s possible to > dynamically update a sortable list by using replace_html, and still > allow the list to be sorted, without manually reloading the page. > Thanks,Well, I managed to solve this problem by putting the following in my controller: render :update do |page| page.replace_html(''sortable_list'', :partial => ''section'', :collection => Section.find_all) page.replace_html(''sortable_list_control'', sortable_element(''sortable_list'', :url => { :action => ''order_section'', :ids => Section.find_all})) seems that once you do an RJS replace_html update, you''ve gotta call Sortable.create(''id_of_container'',[options]); to re-initialize the sortable container (which is what the rails sortable_element method does) Mike