Ruby on Rails sortable list
Posted
by mdgrech
on Stack Overflow
See other posts from Stack Overflow
or by mdgrech
Published on 2009-08-26T17:20:59Z
Indexed on
2010/03/23
20:03 UTC
Read the original article
Hit count: 139
sortable
|ruby-on-rails
I created a sortable list in my RoR project, unfortunately it's not saving the list position. Upon page refresh the items return to their normal spot. I've pasted the code below or you can git it: git://github.com/mdgrech/23notes-.git
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
© Stack Overflow or respective owner