Sortable list (scriptalicious) and updating database


#1

I have made a sortable list which is sortable by dragging & dropping the
list elements. It works, but the order of the elements will not be
updated in the database. Hopefully someone could help me, or tell me
what I’m doing wrong.

Thnx!
Rgds,
Eric

Controller: album_controller.rb
class AlbumController < ApplicationController

def show_sort_album
@photos = Photo.find(:all)
end

def sort
@photos.each do |photo|
photo.position = params[‘photo-list’].index(photo.id.to_s) + 1
photo.save
end
render :nothing => true
end
end

Model: photo.rb
class Photo < ActiveRecord::Base
end

Views: show_sort_album.rhtml (in album)

    <% @photos.each do |photo| %>
  • <%= photo.name %>
  • <%end%>

<%= sortable_element ‘photo-list’,
:url => { :action => “sort” },
:complete => visual_effect(:highlight, ‘photo-list’)
%>

A layout file includes a necessary javascript tags. The database
contains a table ‘photos’ with the following fields: id, name,
image_filename, thumb_filename, position


#2

On Sunday 19 February 2006 3:46 am, Eric Snaak wrote:

class AlbumController < ApplicationController
render :nothing => true

    :url => { :action => "sort" }, :complete => visual_effect(:highlight, 'photo-list')

    %>

    A layout file includes a necessary javascript tags. The database
    contains a table ‘photos’ with the following fields: id, name,
    image_filename, thumb_filename, position

At first glance @photos isn’t assigned to anything in your sort method.
Also,
in this case the wiki is your friend:
http://wiki.rubyonrails.com/rails/pages/HowToUseDragAndDropSorting

-Carl


#3

@Carl

It works now!

Thnx!