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