Created
June 12, 2009 17:29
-
-
Save robinsp/128779 to your computer and use it in GitHub Desktop.
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
<!-- partial --> | |
<% content_tag :li, :id => dom_id(root) do %> | |
<%= content_tag :span, root.name %> | |
<%content_tag :ul do %> | |
<% for child in root.direct_children do %> | |
<%= render :partial => "tree", :locals => {:root => child}%> | |
<%end %> | |
<%end unless root.direct_children.empty? %> | |
<%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
<!-- index view --> | |
<h1>Items</h1> | |
<ul id="my_item_tree"> | |
<%= render :partial => "tree", :locals => {:root => @root} %> | |
</ul> | |
<%= sortable_element_reporting_target "my_item_tree", "moved_item_id", | |
:tree => true, | |
:url => sort_items_path, | |
:method => :put %> |
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
# Model | |
class Item < ActiveRecord::Base | |
acts_as_nested_set | |
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
# Controller | |
class ItemsController < ApplicationController | |
handles_sorting_of_nested_set | |
def index | |
@root = Item.find_by_parent_id(nil) | |
end | |
def sort | |
new_position = position_of(:moved_item_id).in_tree(:my_item_tree) | |
# This is where you should update your model and | |
# return som javascript to update the view. | |
# In this sample, we settle for logging the new position and rendering nothing. | |
logger.info("New position of item #{params[:moved_item_id]}: " + new_position.inspect) | |
render :nothing => true | |
end | |
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
# routes.rb | |
ActionController::Routing::Routes.draw do |map| | |
map.resources :items, :collection => {:sort => :put} | |
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
ul { | |
list-style: none; | |
} | |
li > ul { | |
padding-left: 1em; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment