# Sortable list (scriptalicious) and updating database

**URL:** <https://www.ruby-forum.com/t/sortable-list-scriptalicious-and-updating-database/70382>\
**Category:** Rails\
**Created:** [February 19, 2006, 10:46am UTC](https://www.ruby-forum.com/t/sortable-list-scriptalicious-and-updating-database/70382 "2006-02-19T10:46:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [February 19, 2006, 10:46am UTC](https://www.ruby-forum.com/t/sortable-list-scriptalicious-and-updating-database/70382/1 "2006-02-19T10:46:27Z")

</div>

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 %\> ![](https://www.ruby-forum.com/images/thumbs/<%=%20%0Aphoto.image_filename%20%>)
\<%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

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [February 19, 2006, 11:59am UTC](https://www.ruby-forum.com/t/sortable-list-scriptalicious-and-updating-database/70382/2 "2006-02-19T11:59:59Z")

</div>

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](http://wiki.rubyonrails.com/rails/pages/HowToUseDragAndDropSorting)

-Carl

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [February 19, 2006, 12:45pm UTC](https://www.ruby-forum.com/t/sortable-list-scriptalicious-and-updating-database/70382/3 "2006-02-19T12:45:17Z")

</div>

@Carl

It works now!

Thnx!
