2012-05-30 3 views
0

내 모델 컨트롤러에서 data-update-url을 호출 할 수있는 jquery sortable이 있습니다. 모든 것이 좋으며 함수가 호출됩니다. 몽고 이드 정렬 및 update_all

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

지금 내가 mongoid을 사용하고 있는데 내가 아는 내 정렬 기능 당신이 SQL에서 가능한 한 원하는 것을 할 수있는 유연한 방법은 정확히 없다. 사용자가 원하는대로 목록의 요소를 드래그하면 사용자 목록의 순서가 세션을 통해 유지되도록 positon을 업데이트해야합니다. [: 문서] 위의 기능이 내가 그렇게 시작 템플릿 내가 (railscasts에서) 올바른 방향으로 시작할 수 있습니다 내 첫 번째 문제는 PARAMS입니다했다 .each_with_index을, 메신저 내가 해요 그래서

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

을 던져 gettting 확실히 params [: document]는 each_with_index 메소드로 전달하고자하는 것이 아니라 무엇을 시도해야할지 확신하지 못합니까? ERB에게

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

pleasea 쇼 JS와 뷰 코드를 해당

갱신 document.js.coffee

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

그래서 우리는 누가 –

+0

홀수가, 지금은 무엇을 얻을에 URL을 PARAMS를 채 웁니다 참조 다른 인수, params [: document]는 문서의 해시를 전달하는 것처럼 보이지만 ArgumentError (잘못된 인수 개수 (1에 대해 2))가 발생합니다. 오류가 발생 했으므로 진행이 진행됩니다. n update_all 블록 – TheIrishGuy

답변

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 
+0

이 doc = current_user.documents.find (id)로 변경되었으며, 원하는대로 작동합니다. 감사합니다. – TheIrishGuy