Eric Snaak
2006-Feb-19 09:46 UTC
[Rails] Sortable list (scriptalicious) and updating database
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
--
Posted via http://www.ruby-forum.com/.
Carl Porth
2006-Feb-19 10:58 UTC
[Rails] Sortable list (scriptalicious) and updating database
On Sunday 19 February 2006 3:46 am, Eric Snaak wrote:> 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, positionAt 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