2009-08-26 217 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