How to handle acts_as_list - solution

241 views
Skip to first unread message

mikel

unread,
Jun 19, 2007, 4:18:03 AM6/19/07
to ActiveScaffold : Ruby on Rails plugin
This is a post for documentation purposes as I couldn't find a single
complete answer for this, so worked out out.

Figured someone else may look for it in the future.

OK, say you have a model that you have "acts_as_list" defined. And
you want this list always in order and want to be able to move the
order up and down and move an item to the top or bottom of the list.

This is how you do it (quick unexplained version, I'll put the longer
version on my blog http://www.blognow.com.au/q/ soon)

In your controller put:

class ItemsController < ApplicationController

active_scaffold :items do |config|
config.action_links.add 'up', :label => 'Up', :type
=> :record, :position => false, :method => 'put'
config.action_links.add 'down', :label => 'Down', :type
=> :record, :position => false, :method => 'put'
config.action_links.add 'top', :label => 'Top', :type
=> :record, :position => false, :method => 'put'
config.action_links.add 'bottom', :label => 'Bottom', :type
=> :record, :position => false, :method => 'put'
end

def up
@item = Item.find(params[:id])
@item.move_higher
do_list
render :action => 'move'
end

def down
@item = Item.find(params[:id])
@item.move_lower
do_list
render :action => 'move'
end

def top
@item = Item.find(params[:id])
@item.move_to_top
do_list
render :action => 'move'
end

def bottom
@item = Item.find(params[:id])
@item.move_to_bottom
do_list
render :action => 'move'
end

def move
end
end

Then make one file in /app/views/items/, called "move.rjs" This
contains one line:

page.replace_html active_scaffold_content_id, :partial => "list"


Note, it has to be page.replace_html not page.replace otherwise your
return message gets rendered as escaped HTML, not pretty :)

That's it, now you have a sortable updating list with four buttons on
it (Up Down Top Bottom). Click a button and the position values
change for the whole list. if you have the position column visible
and clicked, it will also dynamically resort the list as well.

One thing I am not sure how to do (guidance would be nice) is how do I
set the column so it is ALWAYS sorted by position in ascending order
(ie, effectively has the position column always clicked). this is
persistent between updates if you click the position column once, but
not sure how to set it as default when you load the page.

These buttons are pretty trivial, maybe they could be bundled in to
show if you have an acts_as_list model. Voila, instant acts as list
support :)

Regards

Mikel

Daniel E

unread,
Jun 19, 2007, 8:23:25 AM6/19/07
to ActiveScaffold : Ruby on Rails plugin
Thanks for this. It is a feature I was looking at implementing in my
app today. :)
I'll look at rolling this functionality up into a quick plugin and
post it on the wiki.

As far as default sorting, you want the config.list.sorting =
[:column, :other] property.
http://activescaffold.com/docs/api-list

mikel

unread,
Jun 19, 2007, 8:46:30 AM6/19/07
to ActiveScaffold : Ruby on Rails plugin
Hey no problems. Haven't really got my head around plugins yet. Only
started with Rails a few weeks ago.

Good to see if you can put it into a plugin :) Then I can see how you
did it too!

Oh.. and the config.list.sorting was exactly what I needed... can't
belived I didn't se eit before.

regards

Mikel

Daniel E

unread,
Jun 20, 2007, 12:12:29 AM6/20/07
to ActiveScaffold : Ruby on Rails plugin

Differenthink

unread,
Jul 24, 2007, 12:43:02 PM7/24/07
to ActiveScaffold : Ruby on Rails plugin
Hum it seems that the wiki page was removed :/... any idea why ?
i would like to have a doc about this plugin, i guess it was on that
wiki... it could help other people to get it back on ..

thanks

On Jun 20, 6:12 am, Daniel E <deinspan...@gmail.com> wrote:
> http://wiki.activescaffold.com/wiki/show/ActsAsListReorderingActionPl...http://groups.google.com/group/activescaffold/web/active_scaffold_act...

Lance Ivy

unread,
Jul 24, 2007, 12:54:36 PM7/24/07
to actives...@googlegroups.com
looks like someone spammed the wiki page, so i did a rollback.

Differenthink

unread,
Jul 24, 2007, 6:11:01 PM7/24/07
to ActiveScaffold : Ruby on Rails plugin
great,
many thx

On Jul 24, 6:54 pm, "Lance Ivy" <la...@cainlevy.net> wrote:
> looks like someone spammed the wiki page, so i did a rollback.
>

> On 7/24/07, Differenthink <guillaume.mont...@gmail.com> wrote:
>
>
>
> > Hum it seems that the wiki page was removed :/... any idea why ?
> > i would like to have a doc about this plugin, i guess it was on that
> > wiki... it could help other people to get it back on ..
>
> > thanks
>
> > On Jun 20, 6:12 am, Daniel E <deinspan...@gmail.com> wrote:
>

> >http://wiki.activescaffold.com/wiki/show/ActsAsListReorderingActionPl....

Mikel Lindsaar

unread,
Jul 24, 2007, 7:22:47 PM7/24/07
to actives...@googlegroups.com

Kosmas

unread,
Jul 25, 2007, 6:08:46 AM7/25/07
to ActiveScaffold : Ruby on Rails plugin
Just downloaded the plugin, followed the instructions, on the wiki,
but when I try to restart the webbrick server I have the following
error:

====================================================
Exiting
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
core_ext/module/aliasing.rb:28:in `alias_method': undefined method
`active_scaffold_includes' for module
`ActiveScaffold::Helpers::ViewHelpers' (NameError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/core_ext/module/aliasing.rb:28:in `alias_method_chain'
from script/../config/../vendor/plugins/
active_scaffold_acts_as_list/lib/helpers/view_helpers.rb:10
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:488:in `load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:488:in `load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:342:in `new_constants_in'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:488:in `load'
from script/../config/../vendor/plugins/
active_scaffold_acts_as_list/init.rb:8:in `load_plugin'
from /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/
initializer.rb:401:in `load_plugin'
... 35 levels...
from /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/
server.rb:39
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
from ./script/server:3
===============================================================================

I believe that the two following lines cause the problem?
alias_method_chain :active_scaffold_includes, :aal
alias_method_chain :render_action_link, :aal

If I comment them out the webbrick server starts, but then the plugin
doesn't work.

Thanks

Daniel E

unread,
Jul 25, 2007, 9:33:09 AM7/25/07
to ActiveScaffold : Ruby on Rails plugin
I believe that sounds like you aren't running with an ActiveScaffold
trunk release? Are you running with 1.0.1?

Kosmas

unread,
Jul 25, 2007, 10:34:02 AM7/25/07
to ActiveScaffold : Ruby on Rails plugin
Spot on Daniel
Yes I am running the 1.0.1 release.
I haven't noticed the later than 1.0.1 line on the wiki !!

I have exported the trunk version, and renamed the 1.0.1
active_scaffold to bk_active_scaffold, but I'm getting a different
error now.

=============================================
NoMethodError (undefined method `exclude' for
#<ActiveScaffold::DataStructures::Columns:0xb693fe20>):
.//vendor/plugins/active_scaffold/lib/configurable.rb:19:in
`method_missing'
.//vendor/plugins/active_scaffold/lib/config/core.rb:96:in
`initialize'
.//vendor/plugins/active_scaffold/lib/active_scaffold.rb:47:in
`new'
=========================================

Lance Ivy

unread,
Jul 25, 2007, 12:09:53 PM7/25/07
to actives...@googlegroups.com
It's kinda dangerous to have two versions of ActiveScaffold in your /vendor/plugins directory. They're both going to get loaded no matter what you name them.

Kosmas

unread,
Jul 26, 2007, 4:13:03 AM7/26/07
to ActiveScaffold : Ruby on Rails plugin
Thanks Lance,

!! Didn't know about it, but I've discovered it when most things were
not working :-(....
Now I know..
Won't do it again :-)

Kosmas

unread,
Jul 26, 2007, 4:20:48 AM7/26/07
to ActiveScaffold : Ruby on Rails plugin
By the way, I've tried to get to the page for the instructions , but
it's been spammed again :-(..

On Jul 25, 5:09 pm, "Lance Ivy" <la...@cainlevy.net> wrote:

> It's kinda dangerous to have two versions of ActiveScaffold in your
> /vendor/plugins directory. They're both going to get loaded no matter what
> you name them.
>

Daniel E

unread,
Jul 26, 2007, 7:23:50 AM7/26/07
to ActiveScaffold : Ruby on Rails plugin
When this happens, you can fix it yourself by clicking the "Back in
time" link at the bottom, and when you find the unspammed version,
click the Rollback link.

Kosmas

unread,
Jul 26, 2007, 10:15:36 AM7/26/07
to ActiveScaffold : Ruby on Rails plugin
Thanks Daniel,

Now that I know I'll do that whenever it happens again.
Cheers..

gui...@gmail.com

unread,
Aug 3, 2007, 10:49:48 PM8/3/07
to ActiveScaffold : Ruby on Rails plugin
Hi Everyone.

I've been reading the old messages from this group to help me get
started. I finally got active_scaffold running from the trunk release.
I chose the trunk release because I want to use
active_scaffold_acts_as_list.

But I'm finding that with the plugin for active_scaffold_acts_as_list,
I get always an error message if my class acts_as_list.

For example I have a bottle class, it's very simple...

class Bottle < ActiveRecord::Base
acts_as_list
end

and when I try to view the bottles this is the error I get.

DBI::DatabaseError: Execute
OLE error code:80040E14 in Microsoft OLE DB Provider for SQL
Server
Incorrect syntax near '\.'.
HRESULT error code:0x80020009
Exception occurred.: SELECT * FROM (SELECT TOP 15 * FROM (SELECT
TOP 15 * FROM bottles ORDER BY bottles.[position] ASC) AS tmp1 ORDER
BY bottles\.\[[position]\] DESC) AS tmp2 ORDER BY bottles\.\[[position]
\] ASC

I don't get this error message when I remove the plugin -- so I was
wondering if maybe there is some sort of bug in the
active_scaffold_acts_as_list plugin?

I am running mongrel rails on windows, and connecting to MS SQLServer
(sorry). The ado.rb file in root:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/
ADO.rb is up to date.

Thanks in advance for any tips on how I can get this working.

:)

gui...@gmail.com

unread,
Aug 3, 2007, 11:12:43 PM8/3/07
to ActiveScaffold : Ruby on Rails plugin
On further analysis, the SQL syntax error seems to originate from the
active_scaffold trunk files.

The latest version of the active_scaffold plugin works... but the
latest version doesn't work with active_scaffold_acts_as_list. :(

That's about all I know, I wish I could fix it!

Lance Ivy

unread,
Aug 4, 2007, 2:39:22 AM8/4/07
to actives...@googlegroups.com
Have you tried the patched SQL Server adapter? See http://groups.google.com/group/activescaffold/files/.

Daniel E

unread,
Aug 4, 2007, 11:48:01 AM8/4/07
to ActiveScaffold : Ruby on Rails plugin
Yeah, there is a bug actually in ActiveRecord where it escapes some
stuff poorly.
As Lance suggested, I'd recommend the full SQL Server patch. It is a
bit slower of a method for pagination, but it is much less hairy than
the current patches and workarounds you have to make to get SQLServer
to play nicely with the calls AS is making.

Daniel

On Aug 4, 2:39 am, "Lance Ivy" <la...@cainlevy.net> wrote:
> Have you tried the patched SQL Server adapter? Seehttp://groups.google.com/group/activescaffold/files/.
>

Reply all
Reply to author
Forward
0 new messages