尝试将嵌套排序纳入我的rails应用程序。我正在使用蒙古和祖先的宝石,我试图做一个页面,更新一个类别的parent_id
的拖放与嵌套排序。
一直在尝试,但不真正理解NestedSortable为了创建一个可以读取它的控制器而吐出的东西。
My controller has:
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'))
我的reorder视图吐出一些像:
<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>
在认真地摆弄了我从嵌套排序中得到的数组之后,我能够通过在我的控制器中使用这个来使这个工作:
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调试语句。
干杯!