2013-03-12 41 views
1

試圖將嵌套可排序到我的Rails應用程序。 我正在使用Mongoid和Ancestry gem,並且我正在嘗試使用Nested-Sortable進行拖放時更新parent_id類別的頁面。使用NestedSortable與Ancestry gem和Mongoid

一直在嘗試做實驗,但並沒有真正理解NestedSortable吐出來創建一個將讀取它的控制器。

我的控制器有:

def sort 
    Category.update_all({parent_id: params[:parent_id].to_i}) 
    render nothing: true 
end 

路線:

resources :symbols, :as => :categories, :controller => :categories do 
    collection {post :sort} 
end 

Reorder.js.coffee:

jQuery -> 
    $('.reorder-tree').nestedSortable 
    handle: '.handle' 
    items: 'li' 
    toleranceElement: '> div' 
    update: -> 
     $.post($(this).data('update-url'), $(this).nestedSortable('serialize')) 

我的訂貨觀點吐出東西這樣的:

<ol class=reorder-tree> 
<li id="category_513d372b10188f9b6b000014"><div> 
    <i class="icon-move handle"></i> 
    <a href="/symbols/chickne">Chickne</a> 
    <span class="btn-group show-on-hover"> 
    <a href="/symbols/new?parent_id=chickne" class="btn btn-micro" data-remote="true" title="Add Sub-Category"><i class="icon-plus"></i> 
    </a> 
    <a href="/symbols/chickne/edit" class="btn btn-micro"><i class="icon-pencil"></i> 
    </a> 
    <a href="/symbols/chickne" class="btn btn-micro" data-method="delete" data-remote="true" rel="nofollow" title="Delete Category"><i class="icon-remove"></i> 
    </a> 
    </span> 
</div> 
<ol class="visible"><li id="category_513d373310188f9b6b000016"><div> 
    <i class="icon-move handle"></i> 
    <a href="/symbols/smoking-babies">Smoking Babies</a> 
    <span class="btn-group show-on-hover"> 
    <a href="/symbols/new?parent_id=smoking-babies" class="btn btn-micro" data-remote="true" title="Add Sub-Category"><i class="icon-plus"></i> 
    </a> 
    <a href="/symbols/smoking-babies/edit" class="btn btn-micro"><i class="icon-pencil"></i> 
    </a> 
    <a href="/symbols/smoking-babies" class="btn btn-micro" data-method="delete" data-remote="true" rel="nofollow" title="Delete Category"><i class="icon-remove"></i> 
    </a> 
    </span> 
</div> 
<ol class="hide"></ol> 
</li> 
</ol></li> 
</ol> 

任何幫助,將不勝感激,謝謝。

回答

0

嚴重玩弄我是從嵌套可排序得到陣列後, 我能夠在我的控制器使用這個來完成這項工作:

def sort 
    # html = "Env: #{Rails.env}. " 
    # infor = params[:category] 
    params[:category].each do |id, attr| 
     thisCat = params[:category][id] 
     @category = Category.where(:_id => id).first 
     # html << "ID= #{id} , thisCat= #{thisCat} Name= #{@category.name} Parent= #{@category.parent_id} ; " 
     unless thisCat == "null" 

     @category.parent_id = thisCat.to_s 
     @category.save 
     end 
    end 
    # flash[:alert] = html 
    # flash[:error] = infor 
    # render nothing: true 
    end 

你可以看到我註釋掉我得到了後它的工作;這就是我所有的調試Flash語句。

乾杯!