2009-08-26 18 views
2

Ich habe eine sortierbare Liste in meinem RoR-Projekt erstellt, leider speichert es nicht die Listenposition. Wenn die Seite aktualisiert wird, kehren die Elemente an ihren normalen Ort zurück. Ich habe den Code unten eingefügt oder Sie können es git: git: //github.com/mdgrech/23notes-.gitRuby on Rails sortierbare Liste

app/views/notes/index.html.erb 
///////////////////////////////////////////////////////////////////////////////////////////// 
<div id="newNoteDiv"></div> 

<ul id="notesList"> 
    <% for note in @notes %> 
     <li id="<%=h note.position %>"> 
     <span class="handle">[drag]</span> 
     <div id="listContent"> 
     <h3><%= link_to note.title, edit_note_path(note) %></h3> 
     <p><%=h note.content %></p> 
     <%= link_to "Destroy", note, :confirm => 'Are you sure?', :method => :delete %> 
     </div> 
     </li> 
    <% end %> 
</ul> 

<%= sortable_element("notesList", :url => sort_notes_path, :handle => "handle") %> 

app/controllers/notes_controller.rb 
////////////////////////////////////////////////////////////////////////////////////////// 
    def index 
    @notes = Note.all(:order => "position") 
    end 

    def sort 
    params[:notes].each_with_index do |id, index| 
     Note.update_all(['position=?', index+1], ['id=?', id]) 
    end 
    render :nothing => true 
    end 

config/routes.rb 
////////////////////////////////////////////////////////////////////////////////////////// 
    map.resources :notes, :collection => { :sort => :post } 
    map.root :notes 
app/models/note.rb 
////////////////////////////////////////////////////////////////////////////////////////// 
class Note < ActiveRecord::Base 
    acts_as_list 
end 
+0

Was sehen Sie im Protokoll, wenn Sie ein Element in der Liste verschieben? –

Antwort

0

Ok habe es, die ID Sie zum ul zuweisen, die sortable_element Sie wählen und params, die Sie angeben, sollen alle gleich sein, dh:

<ul id="foofooberry" 
... 
</ul> 

<%= sortable_element('foofooberry', :url => sort_notes_path) %> 

    def sort 
    params[:foofooberrry].each_with_index do |id, index| 
     Note.update_all(['position=?', index+1], ['id=?', id]) 
    end 
    render :nothing => true 
    end