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

リスト内の項目を移動すると、何が表示されますか? –

答えて

0

[OK]を、それを得た、idがあなたは、UL、あなたが選択しsortable_elementに割り当てますあなたが指定するパラメータはすべて同じであるはずです:

<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 
関連する問題