Topic: problems with ajax draggable list

I am having trouble updating the position column of a todo list that I created. I am using script.aculo.us for the draggable feature which works fine but I need to update the position column in the db for each item in the list when one is moved.

Any ideas what the problem is?


*** Controller (todo_conroller)
 
def update_not_done_positions
  params[:not_done_items].each_with_index do |id, position|
    Todo.update(id, :position => position)
  end
  render :nothing => true
end


*** View (list.rhtml)

<% unless @not_done.empty? %>
    Not Done:<br />
    <ul id="not_done_items" style="list-style-type: none">
    <%= render_collection_of_partials "display", @not_done %>
    </ul>

    <p> <hr size=3> </p>
       
    <%= sortable_element 'not_done_items',
              :complete => visual_effect(:highlight, 'not_done_items'),
              :url => { :conroller => "todo", :action => "update_not_done_positions" } %>
<% end %>


*** Partial (_display.rhtml)
<% @item = display %>

<li id="<%= @item.position %>">
<%= check_box("item", "done", "onclick" => "document.location.href='/todo/toggle_check/#{@item.id}'") %>
<%= @item.description %>
<%= link_to("Edit", :action => "edit", :id => @item.id) %>
<%= link_to("Delete", { :action => "delete", :id => @item.id },
:confirm => "Are you sure you want to delete this entry: #{@item.description}") %>
</li>

Re: problems with ajax draggable list

A couple things, I think you need to add one to the position here:

Todo.update(id, :position => (position+1))

Try the id of the item in the li id attribute. You may need to prefix it too:

<li id="item_<%= @item.id %>">

Everything else looks correct. If it still isn't working, can you give any details on what is wrong? Take a look at the development.log file to see the parameters that are being passed on the request.

Railscasts - Free Ruby on Rails Screencasts

Re: problems with ajax draggable list

once again you are right on...

while I didn't "have" to add one, it makes sense or else the list starts with 0.

prefixing the item id with item_ (<li id="item_) actually makes the js that effects the display work as an added bonus smile

Also I figured out I was using the item position where I should have been using the item id
(<li id="item_<%= @item.id %>"> instead I was doing <li id="item_<%= @item.position %>"> which in no way helps me find the right todo item in the db wink

Again thanks!

(oh and thanks for the tip to look in the development log, that helped me figure out the position versus id mistake - good knowledge for the future)

Last edited by rburbach (2007-01-07 04:59:10)