RailsCasts Pro episodes are now free!
Learn more or hide this
There is a newer version of this episode, see the revised episode.
Resources
script/generate migration add_position_to_faqs position:integer rake db:migrate script/plugin install git://github.com/rails/acts_as_list.git
map.resources :faqs, :collection => { :sort => :post }
def sort params[:faqs].each_with_index do |id, index| Faq.update_all(['position=?', index+1], ['id=?', id]) end render :nothing => true end
class Faq < ActiveRecord::Base acts_as_list end
<%= javascript_include_tag :defaults %>
<ul id="faqs"> <% for faq in @faqs %> <% content_tag_for :li, faq do %> <span class="handle">[drag]</span> <%= link_to h(faq.question), faq %> <% end %> <% end %> </ul> <%= sortable_element("faqs", :url => sort_faqs_path, :handle => "handle") %>
li .handle { font-size: 12px; cursor: move; color: #777; }