2009-08-26 142 views
2

我在我的RoR項目中創建了一個可排序列表,很不幸,它不保存列表位置。頁面刷新後,項目返回到正常位置。我已經粘貼下面的代碼,也可以它的git:git的://github.com/mdgrech/23notes-.gitRuby on Rails排序列表

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

你在日誌中看到,當你移動到列表中的項目? – 2009-08-26 17:29:54

回答

0

好了它,分配給UL的ID,您選擇的sortable_element ,而params指定都應該是相同的,即:

<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