acts_as_list
功能:排序
使用难度:简单
fork数:295
安装:
gem 'acts_as_list'
使用:
rails g migration AddPositionToTodoItem position:integer
rake db:migrate
class TodoList < ActiveRecord::Base
has_many :todo_items, -> { order(position: :asc) }
end
class TodoItem < ActiveRecord::Base
belongs_to :todo_list
acts_as_list scope: :todo_list
end
todo_list = TodoList.find(...)
todo_list.todo_items.first.move_to_bottom
todo_list.todo_items.last.move_higher
注意:
建立方法move_higher:
def move_higher
todo_list.todo_items.first.move_higher
end
建立路径:
resources todo_items do
get :move_higher
end
建立方法move_to_bottom:
def move_to_bottom
todo_list.todo_items.first.move_to_bottom
end
建立路径:
resources todo_items do
get :move_to_bottom
end