0

I followed Ryan Bates' rails tutorial on setting up a sortable list of objects. With some modifications, it's now working to the point where I can drag and drop a list of "belongs to" elements on a 'has many' page, and the post action is sent to my controller... Which then gives a 500 error

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each_with_index): app/controllers/cuttings_controller.rb:87:in `sort'

I believe this is because while Ryan's controller knew which cutting it's dealing with, mine needs the parameters passed to it.

Here is the line on my users page (which holds 'cuttings'):

<%= sortable_element('faqs', :url => 'sort_cutting', :handle => "handle") %>

And here is the definition in the cuttings controller:

  def sort
    params[:cuttings].each_with_index do |id, index|
      Cutting.update_all(['position=?', index+1], ['id=?', id])
    end
    render :nothing => true
  end

Would somebody be kind enough to tell me how to tweak this so it will work properly? Which parameters should I pass? I've tried :cutting => @cutting.id, which didn't work. The post route is set up using:

match 'users/sort_cutting' => 'cuttings#sort'

... Which is a bit ugly, but the only way I could get it to work!

Thanks in advance.

Community
  • 1
  • 1
Nick
  • 839
  • 1
  • 10
  • 19

1 Answers1

0

I answered this by reading this post - the params didn't match 'faqs', the sortable element I was trying to deal with. Simple when you know how!

Nick
  • 839
  • 1
  • 10
  • 19