0

モデルコントローラーで data-update-url を呼び出す jquery ソート可能があります。すべて問題なく、関数が呼び出されています。私のソート機能

def sort
        params[:documents].each_with_index do |id, index|
            Document.update_all({position: index+1}, {id: id})
        end
        render nothing: true
    end

今、私はモンゴイドを使用しています.SQLでできるように、私がやりたいことを正確に柔軟に行う方法はないことを知っています. ユーザーがリスト内の要素を好きなようにドラッグした後、位置を更新して、ユーザーリストの順序がセッション全体で維持されるようにします。上記の関数は私が開始したテンプレートだったので、(railscasts から) 正しい方向に開始できます。私の最初の問題は、params[:documents].each_with_index です。

NoMethodError (undefined method `each_with_index' for nil:NilClass):
  app/controllers/documents_controller.rb:16:in `sort'

したがって、params[:document] は each_with_index メソッドに渡したいものではないと確信していますが、何を試したらよいかわかりません。

document.js.coffee を更新する

jQuery ->
  $('#documents').sortable(
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize')));

対応するerb

<ul id="documents" data-update-url="<%= sort_documents_url %>">
  <% @documents.each do |document| %>
    <%= content_tag_for :li, document do %>
      <%= render document %>
    <% end %>
  <% end %>
4

1 に答える 1

1
def sort
    params[:documents].each_with_index do |id, index|
      doc = Document.find_by_id(id)
      doc.update_attribute(:position, index) if doc
    end
    render nothing: true
end
于 2012-05-30T13:45:21.220 に答える