美文网首页
acts_as_list的用法

acts_as_list的用法

作者: 柳辉 | 来源:发表于2018-04-09 18:26 被阅读17次

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

相关文章

网友评论

      本文标题:acts_as_list的用法

      本文链接:https://www.haomeiwen.com/subject/dmaihftx.html