# RailsSortable [![Build Status](https://travis-ci.org/itmammoth/rails_sortable.svg?branch=use_travis_ci)](https://travis-ci.org/itmammoth/rails_sortable) RailsSortable is a simple Rails gem that allows you to create a listing view with drag & drop sorting. The arranged order will be persisted in the table without any pain. ![RailsSortable](https://raw.githubusercontent.com/itmammoth/rails_sortable/master/rails_sortable.gif "RailsSortable") # Setup Add the following to your `Gemfile` then run bundle to install them. ``` gem 'jquery-rails' gem 'jquery-ui-rails' gem 'rails_sortable' ``` And then add the following to the asset pipeline in the `application.js`: ``` //= require jquery //= require jquery_ujs //= require jquery-ui/widgets/sortable //= require rails_sortable ``` # Usage RailsSortable requires a specific column on the ActiveRecord Model for its implementation. For instance, the following migration indicates the case that you are attemtting to make `Item` model sortable. ```ruby class CreateItems < ActiveRecord::Migration[5.1] def change create_table :items do |t| t.string :title t.integer :sort # for RailsSortable t.timestamps end end end ``` and `Item` model as ```ruby class Item < ApplicationRecord include RailsSortable::Model set_sortable :sort # Indicate a sort column # If you do NOT want timestamps to be updated on sorting, use the following option. # set_sortable :sort, without_updating_timestamps: true end ``` and `ItemsController` as ```ruby class ItemsController < ApplicationController def index @items = Item.order(:sort).all end end ``` and the listing view (typically - index.html.erb) as ```erb ...
<%= item.title %> | <%= item.sort %> | <%= link_to 'Show', item %> | <%= link_to 'Edit', edit_item_path(item) %> | <%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %> |