开发者

Sorting deeply nested attributes in Rails

开发者 https://www.devze.com 2022-12-22 00:59 出处:网络
I want to be able to drag and drag App model which is nested under Category model. http://railscasts.com/episodes/196-nested-model-form-part-1

I want to be able to drag and drag App model which is nested under Category model.

http://railscasts.com/episodes/196-nested-model-form-part-1

Here's the Railscast I've tried to follow.

#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

I'm able to sort Categories with something similar, but since I'm updating an attribute, I'm having trouble. This is how I sort the Categories list.

def sort
  params[:categories].each_with_index do |id, index|
    Category.update_all(['position=?', index+1], ['id=?', id])
  e开发者_如何学Cnd
  render :nothing => true
end

Upon further inspection, what I need is to be able to update all apps at the same time, except I can't just do App.update_all, since App is an attribute of category.

I tried using

@category = Category.find(params[:id])
@app = @category.apps.all

But, I'm not passing in the Category id, so it doesn't know which Category it is.

Here it is in my view

%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")

Any help is appreciated.


Turns out it was just a matter of sorting the records by position. I was sorting categories in controller. So for the nested attribute model, I sorted them in model:

has_many :apps, :dependent => :delete_all, :order => "position"

When I move the apps, the position gets updated by simply calling

App.update_all(['position=?', index+1], ['id=?', id])

Then I sort them accordingly in the model. Turns out there was no need to pass in id of category, just update all Apps. But, I'm afraid it might slow up a bit, so if anyone has a better solution, I'm all ears.

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消