Forum: Rails Spinoffs (closed, excessive spam) scriptaculous - dynamic number of categories with sortables

Posted by rabaukendisko (Guest)
on 2008-06-25 20:57
(Received via mailing list)
hello,

i'm new to ruby on rails and am trying to use scriptaculous to sort
list items (notes) into categories - the number of these categories is
dynamic. the number of the sortables (notes) is also dynamic.
i read the tips on this website:
http://jotsandsketches.com/?p=199
and according to that i wrote the following code:

----------------------------------------------------------------------------------------------------------------------------------------------------
html.erb
----------------------------------------------------------------------------------------------------------------------------------------------------

<ul id="category-list">
  <% @categories.each do |category| %>
    <li class="category" id="category_<%= category.id %>">
      <span><%=h category.title %></span>
      <span><%=h category.description %></span>
      <span><%=h category.user_id %></span>
      <span><%= link_to 'Show', category %></span>
      <span><%= link_to 'Edit', edit_category_path(category) %></span>
      <span><%= link_to 'Destroy', category, :confirm => 'Are you
sure?', :method => :delete %></span>
      <span><%= link_to 'Add new Note', :controller =>
"notes", :action => "new", :category_id => category.id %></span>
        <ul class="notes-list" id="notes-list_<%= category.id %>">
          <% category.notes.each do |note| %>
            <li id="note_<%= note.id %>">
              <div>
                <p><%=h note.title %></p>
                <p><%=h note.description %></p>
                <%= link_to 'Show', note %>
                <%= link_to 'Edit', edit_note_path(note) %>
                <%= link_to 'Destroy', note, :confirm => 'Are you
sure?', :method => :delete %>
              </div>
            </li>
          <% end %>
        </ul>
    </li>
  <% end %>
</ul>

<% containment = [] %>
<% @categories.each do |category|
     containment << 'notes-list_' + category.id.to_s
   end
%>

<% @categories.each do |category| %>
  <%= sortable_element 'notes-list_' + category.id.to_s ,
  :constraint => false,
  :url => {:action => "sort_notes", :id => @note},
  :complete => visual_effect(:highlight, 'notes-list_' +
category.id.to_s),
  :containment => containment
  %>
<% end %>

----------------------------------------------------------------------------------------------------------------------------------------------------
controller
----------------------------------------------------------------------------------------------------------------------------------------------------
def sort_notes
    @category_list = Category.find_all_by_user_id(current_user.id)
    @category_list.each do |category|
      if params['notes-list_' + category.id.to_s] then
        sortables = params['notes-list_' + category.id.to_s]
      end
    end
    sortables.each do |id|
      currentitem = Note.find_by_id(id.to_i)
      currentitem.position = sortables.index(id) + 1
      currentitem.lists_of_stuff_column_id = params[:id]
      currentitem.save
    end
    render :nothing => true
  end
----------------------------------------------------------------------------------------------------------------------------------------------------

this doesn't work. i can drag the notes around (from one category to
another, and also within the categories), but it doesn't store changes
of the position in the database.
i think that maybe this line might be wrong:

   :url => {:action => "sort_note", :id => @note},

as i don't really understand where the @note comes from (the
controller behind the whole thing only knows @category).

it would be great if someone could help me. have been trying to solve
the problem for days now :(
many thanks!!
Posted by rabaukendisko (Guest)
on 2008-06-26 02:28
(Received via mailing list)
ok, so fixed it myself.
working code:

--------------------------------------------------
template
--------------------------------------------------
<% @categories.each do |category| %>
  <%= sortable_element 'notes-list_' + category.id.to_s ,
  :constraint => false,
  :url => {:action => "sort_notes", :id => category.id},
  :complete => visual_effect(:highlight, 'notes-list_' +
category.id.to_s),
  :containment => containment
  %>
<% end %>

--------------------------------------------------
controller
--------------------------------------------------
def sort_notes
    sortables = nil
    @category_list = Category.find_all_by_user_id(current_user.id)
    @category_list.each do |category|
      if params['notes-list_' + category.id.to_s] then
        sortables = params['notes-list_' + category.id.to_s]
      end
    end
    sortables.each do |id|
      currentitem = Note.find_by_id(id.to_i)
      currentitem.position = sortables.index(id) + 1
      currentitem.category_id = params[:id]
      currentitem.save
    end
    render :nothing => true
  end
This topic is locked and can not be replied to.