Skip to content

Instantly share code, notes, and snippets.

@raarellano
Last active December 30, 2015 08:49
Show Gist options
  • Save raarellano/7804832 to your computer and use it in GitHub Desktop.
Save raarellano/7804832 to your computer and use it in GitHub Desktop.
Gives order to lists through a drag and drop functionality.
rails g migration add_position_to_projects position:integer
rake db:migrate
resources :projects do
collection { post :sort }
end
def sort
params[:project].each_with_index do |id, index|
Project.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
#Acts as Sortable
acts_as_list
# If it belongs to a realationship
#page.rb
has_many :questions, :order => "position"
#question.rb
acts_as_list :scope => :page
%tbody#projects{"data-update-url" => sort_projects_url}
- @projects.each do |project|
= content_tag_for :tr, project do
%td
%span.handle
[Drag]
%td.image
= image_tag(project.project_images.first.image(:thumb)) if project.project_images.count > 0
%td
= project.title
%td
= link_to 'Show', project, :class => "btn btn-default btn-xs"
= link_to 'Edit', edit_project_path(project), :class => "btn btn-warning btn-xs"
= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-danger btn-xs"
jQuery ->
$('#projects').sortable
axis: 'y'
handle: '.handle'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
/*** Acts as List ***/
.handle{
color: gray;
text-align: center;
}
.handle:hover{
cursor: move;
}
gem 'acts_as_list'
gem 'jquery-ui-rails'
#You may need to restart server after you run bundle install
//= require jquery.ui.all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment