public
Last active

Rails SortableMethods

  • Download Gist
sortable_controller_methods.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# lib/sortable_controller_methods.rb (must then load lib files manually)
# Add in "sortable" controllers:
# include SortableControllerMethods
#
# As in http://railscasts.com/episodes/147-sortable-lists
module SortableControllerMethods
def sort
klass.all.each do |object|
object.position = params[object.class.to_s.downcase].index(object.id.to_s) + 1
object.save
end
render :nothing => true
end
 
# May be common index action (or where sort options are displayed)
def order
instance_variable_set("@" + controller_name, klass.all)
end
 
def klass
controller_name.singularize.camelize.constantize
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.