Forum: Ruby on Rails Error When Sorting

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arch S. (Guest)
on 2006-04-10 08:57
Got a bug I cannot eliminate.  My ajax style sorting looks cool but the
position change is never updated.  The error (at bottom points to line
52 in my controller and I have indicated that line).

#view

 <!-- Question Data -->
  <h2>Test Questions</h2>
  <div id="list_questions">
    <%= render(:partial => "list_questions") %>
  </div>
  <%= sortable_element("sortable_list", :complete =>
visual_effect(:highlight, "sortable_list"), :update => "question", :url
=> {:action => "update_question_positions", :id => @test.id }) %>


#view _list_questions
    <ol id="sortable_list" class="sort">
    <% @test.questions.each do |question| -%>
      <li class="green" id="question_<%= question.id
%>">question.question %>
      </li>
    <% end -%>
    </ol>

#controller
    def update_question_positions
      params[:sortable_list].each_with_index do |id, position| #LINE 52
        Question.update(id, :position => position+1)
      end
        render :action => "show"
    end

#error
Processing TestController#update_question_positions (for 127.0.0.1 at
2006-04-09 21:51:15) [POST]
  Session ID: 7d95944ce75767f42f6d4910fd432687
  Parameters: {"action"=>"update_question_positions", "id"=>"5",
"controller"=>"admin/test"}


NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each_with_index):
    /app/controllers/admin/test_controller.rb:52:in
`update_question_positions'
    C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.0/lib/action_controller/base.rb:908:in
`send'
Arch S. (Guest)
on 2006-04-10 09:10
Wierd.  I changed "sortable_list" to "list" and it works.  Not sure why
"sortable_list" wasn't working . . . I don't see a typo.
This topic is locked and can not be replied to.