I’m following the Backbone Railscasts and the sortable list Railscast, but I’ve a problem to get them together.
Backbone template:
<ul id="faqs" data-update-url="/api/faqs/sort">
</ul>
Backbone view:
render: ->
$(@el).html(@template())
@collection.each(@appendFaqs)
$("#faqs").sortable
axis: 'y'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
Rails Controller:
class FaqsController < ApplicationController
def sort
params[:faq].each_with_index do |id, index|
Faq.update_all({position: index+1}, {id: id})
end
render nothing: true
end
OUTPUT:
Started POST "/api/faqs/sort" for 127.0.0.1 at 2012-06-03 01:17:20 +0200
Processing by FaqsController#sort as */*
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `each_with_index' for nil:NilClass):
app/controllers/faqs_controller.rb:28:in `sort'
I understand that my params are nil. But I don’t understand why in the railscast code project which I’ve clone it is working.
I saw that I should overwrite the toJSON method (backbone), should I go in this way ?
To be honest, I’m out of control.
Railscasts used:
You need to set an id on your
<li>elements:You should read this to a better understanding of the serialize method.