Reputation: 2573
I have a jquery sortable calling a data-update-url in my model controller, everything is good and the function is getting called. my sort function
def sort
params[:documents].each_with_index do |id, index|
Document.update_all({position: index+1}, {id: id})
end
render nothing: true
end
Now i'm using mongoid and I know there isn't exactly as flexible way to do what I want as you can in SQL. After a user drags an element in the list the way he wants, I want the positon to update so the order of the users list is persistent through sessions. The above function was a template I started so I can start in the right direction (from railscasts) My first problem is the params[:documents].each_with_index, im gettting thrown a
NoMethodError (undefined method `each_with_index' for nil:NilClass):
app/controllers/documents_controller.rb:16:in `sort'
So i'm sure params[:document] isn't what i want to pass to the each_with_index method but i'm unsure what to try?
update document.js.coffee
jQuery ->
$('#documents').sortable(
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize')));
corresponding erb
<ul id="documents" data-update-url="<%= sort_documents_url %>">
<% @documents.each do |document| %>
<%= content_tag_for :li, document do %>
<%= render document %>
<% end %>
<% end %>
Upvotes: 0
Views: 830
Reputation: 8894
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
Upvotes: 1