2
我希望能夠拖拽和拖拽嵌套在Category模型下的App模型。在Rails中排序深層嵌套的屬性
這裏的Railscast我試圖效仿。
#Category controller
def move
params[:apps].each_with_index do |id, index|
Category.last.apps.update(['position=?', index+1], ['id=?', Category.last.id])
end
render :nothing => true
end
我可以使用類似的東西對類別進行排序,但由於我正在更新屬性,所以遇到了麻煩。這是我如何排序類別列表。
def sort
params[:categories].each_with_index do |id, index|
Category.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
經進一步檢查,我需要的是能夠同時更新所有的應用程序,但我不能只是做App.update_all,因爲應用程序是類的屬性。
我嘗試使用
@category = Category.find(params[:id])
@app = @category.apps.all
但是,我沒有傳入類別ID,所以它不知道它是哪一類。
這在我看來是
%ul#apps
- for app in @category.apps
- content_tag_for :li, app do
%span.handle
[drag]
= h app.title
= sortable_element("apps", :url => move_categories_path, :handle => "handle")
任何幫助表示讚賞。