Forum: Ruby on Rails Sortable list (scriptalicious) and updating database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric S. (Guest)
on 2006-02-19 11:46
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)
<ul id="photo-list">
<% @photos.each do |photo| %>
<li id="item_<%= photo.id %>"> <%= photo.name %>
	<img width="100" height="75" src="/images/thumbs/<%=
photo.image_filename %>"/>
</li>
<%end%>
</ul>

<%= 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
Carl P. (Guest)
on 2006-02-19 12:59
(Received via mailing list)
On Sunday 19 February 2006 3:46 am, Eric Snaak wrote:
> class AlbumController < ApplicationController
>      render :nothing => true
> <ul id="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


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/HowToUseDr...

-Carl
Eric S. (Guest)
on 2006-02-19 13:45
@Carl

It works now!

Thnx!
This topic is locked and can not be replied to.