#Acts as List
Last active
December 30, 2015 08:49
-
-
Save raarellano/7804832 to your computer and use it in GitHub Desktop.
Gives order to lists through a drag and drop functionality.
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
rails g migration add_position_to_projects position:integer | |
rake db:migrate |
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
resources :projects do | |
collection { post :sort } | |
end |
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
def sort | |
params[:project].each_with_index do |id, index| | |
Project.update_all(['position=?', index+1], ['id=?', id]) | |
end | |
render :nothing => true | |
end |
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
#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 |
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
%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" |
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
jQuery -> | |
$('#projects').sortable | |
axis: 'y' | |
handle: '.handle' | |
update: -> | |
$.post($(this).data('update-url'), $(this).sortable('serialize')) |
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
/*** Acts as List ***/ | |
.handle{ | |
color: gray; | |
text-align: center; | |
} | |
.handle:hover{ | |
cursor: move; | |
} |
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
gem 'acts_as_list' | |
gem 'jquery-ui-rails' | |
#You may need to restart server after you run bundle install | |
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
//= require jquery.ui.all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment