Created
June 17, 2011 03:31
-
-
Save tute/1030815 to your computer and use it in GitHub Desktop.
Rails SortableMethods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment