Lookup pattern in Ruby


#1

Hi folks,

I’m curious how a lookup with a large group of values would be handled
in Rails.

For example, I have a (contrived) AnimalType filtering a list of many
Animals–more than can be presented comfortably in a dropdown box.
Therefore I need to either redirect to another screen to select an
animal (returning after the selection is made), or show a dialog–modal
or otherwise–to make the selection. The latter might be presented as a
pagable grid, or perhaps as a grid with a textbox or dropdown to filter
search results.

Is there a convention for this in Rails? I looked at BaseCamp for
examples but only found small groups of lookup values handled easily by
simple dropdowns.

A third option would be DHTML magic to dynamically populate a magic
pseudo-window (the way some people handle searches), but I’m looking for
a way to handle the lookup when this isn’t an option.

Any ideas?


#2

Mike wrote:

Hi folks,

I’m curious how a lookup with a large group of values would be handled
in Rails.

Sounds like a candidate for an AJAX autocomplete text box.

_Kevin


#3

Kevin,

Thanks. Unfortunately #3 isn’t an option in this case. Looking for the
more conventional solution but haven’t found an example yet, as I’m
trying to learn the Rails Way of Doing Things. :wink:

[And it could be that DHTML is the Rails Way!]

Kevin O. wrote:

Mike wrote:

Hi folks,

I’m curious how a lookup with a large group of values would be handled
in Rails.

Sounds like a candidate for an AJAX autocomplete text box.

_Kevin


#4

Mike wrote:

Kevin,

Thanks. Unfortunately #3 isn’t an option in this case. Looking for the
more conventional solution but haven’t found an example yet, as I’m
trying to learn the Rails Way of Doing Things. :wink:

[And it could be that DHTML is the Rails Way!]

In my experience once you get more than say 20 items in a selection box,
it just gets way too cumbersome for a select or multiselect control. To
keep the number of items reasonable you would have to break up your list
somehow.

If there is a secondary attribute somewhere (like mammals, amphibians,
etc… to extend your example), then you could filter the choices in a
select to those that match a category.

You would still need some JS to make the page reload once a category was
selected so it could rebuild the species select box.

Bottom line is that the only good solution is an autocomplete box or an
expandable tree control… both of which require DHTML.

_Kevin


#5

On Thu, 2006-01-26 at 03:21 +0100, Kevin O. wrote:

In my experience once you get more than say 20 items in a selection box,

Bottom line is that the only good solution is an autocomplete box or an
expandable tree control… both of which require DHTML.


can you provide any pointers/references to doing this in rails?

I am struggling with this issue and haven’t gotten any replies so
evidently I am not trying to accomplish it in a popular way.

Craig


#6

I’m having trouble running the plugin script if a rails environment was
generated on a different machine than my development machine. Looks like
something with how the environment vars are setup when you generate a
rails framework.

Any tips on how to make this consistent across systems or at least how
to modify for a different platform, perhaps I can just ignore it in svn.

this is my output:

% ruby script/plugin discover
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/plugin.rb:339:in
initialize': undefined methodroot’ for nil:NilClass (NoMethodError)
from
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/plugin.rb:427:in
new' from /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/plugin.rb:427:inparse!’
from
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/plugin.rb:823
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in require__' from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
from
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
`require’
from script/plugin:3

/j


#7

Doh! I had not setup my environment.rb, oops!


#8

On Thu, 2006-01-26 at 04:23 +0100, Kevin O. wrote:

evidently I am not trying to accomplish it in a popular way.

And page 388-394 or so in Agile Web D. with Rails


The first one I’m not getting either - all the examples seem to be
single table stuff and I am dealing with 2 tables which evidently makes
a difference that I am struggling to resolve (and yes, I am rapidly
losing hair).

I have opened the book to 388 (I was gonna leave Ajax until later) but I
will deal with it now.

This is a sad commentary on rails though…it should be really simple to
get a pop-up select list from the values of one table into a form for
another table. This should not be hard. I’ve spent way too many hours
trying to get what I consider basic functionality to work. The wiki
pages have little useful information on this too.

Craig


#9

James Ho wrote:

Doh! I had not setup my environment.rb, oops!

What do you mean by this? I’m having a similar problem…

b


#10

Craig W. wrote:

On Thu, 2006-01-26 at 03:21 +0100, Kevin O. wrote:

In my experience once you get more than say 20 items in a selection box,

Bottom line is that the only good solution is an autocomplete box or an
expandable tree control… both of which require DHTML.


can you provide any pointers/references to doing this in rails?

I am struggling with this issue and haven’t gotten any replies so
evidently I am not trying to accomplish it in a popular way.

Craig

I’m afraid the conventional technique would probably be more work and
you wouldn’t have much hair left when you got it working well.

Try these…
http://www.ccjr.name/blog/2005/10/16/rails-ajax-autocomplete/

http://www.slash7.com/articles/2005/08/13/ajaxariffic-autocomplete-with-scriptaculous

And page 388-394 or so in Agile Web D. with Rails

_Kevin


#11

On Thu, 2006-01-26 at 04:23 +0100, Kevin O. wrote:

evidently I am not trying to accomplish it in a popular way.

And page 388-394 or so in Agile Web D. with Rails


Ajax hasn’t simplified things…I still cannot make it work - but at
least I now have 3 different methodology failures under my belt.

Error reported in browser
NoMethodError in Clients#edit
Showing app/views/clients/_form.rhtml where line #22 raised:

undefined method `case_manager_with_auto_complete’ for
#<#Class:0xb7a7f594:0xb7a7f4e0>

Clients…
app/layouts/clients.rhtml #starts with

<%= javascript_include_tag "prototype" %>

app/views/clients/_form.rhtml #has line 22 where from which error
stems…
<% case_manager_with_auto_complete :id, :name %>

app/controllers/clients_controller #has line
auto_complete_for :case_manager, :name

DDL #for completeness
CREATE TABLE case_managers (
id serial NOT NULL,
name character varying(53) NOT NULL
);

CREATE TABLE clients (
id serial NOT NULL,
first_name character varying(25) NOT NULL,
middle_initial character varying(1) NOT NULL,
last_name character varying(25) NOT NULL,
case_manager_id integer NOT NULL,
case_manager character varying(53)
);


#12

I was missing my config/environment.rb file.

If you look at script/plugin:3 it requires commands/plugin, this is part
of the rails gem libs; not the rails framework.

On my system it is located in
/usr/lib64/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/plugin.rb

Without proper environment configuration the rails framework would not
know where to find this. Make sure you have created a environment.rb
file and that it is generating the correct environment for your
platform.

Hope this helps.

/j


#13

Craig W. wrote:

Ajax hasn’t simplified things…I still cannot make it work - but at
least I now have 3 different methodology failures under my belt.

I’ll see if I can’t hack something together to work for this, but it may
be a couple of days.

_Kevin


#14

On Thu, 2006-01-26 at 06:46 +0100, Kevin O. wrote:

Craig W. wrote:

Ajax hasn’t simplified things…I still cannot make it work - but at
least I now have 3 different methodology failures under my belt.

I’ll see if I can’t hack something together to work for this, but it may
be a couple of days.


that would be great - I find it hard to believe that more people aren’t
groaning over this…this is pretty basic database stuff.

Craig


#15

On Wed, 2006-01-25 at 22:25 -0700, Craig W. wrote:

I am struggling with this issue and haven’t gotten any replies so
http://www.slash7.com/articles/2005/08/13/ajaxariffic-autocomplete-with-scriptaculous
undefined method `case_manager_with_auto_complete’ for
<% case_manager_with_auto_complete :id, :name %>

CREATE TABLE clients (
id serial NOT NULL,
first_name character varying(25) NOT NULL,
middle_initial character varying(1) NOT NULL,
last_name character varying(25) NOT NULL,
case_manager_id integer NOT NULL,
case_manager character varying(53)
);


my thinking on this is that the problem is with the definition in
clients_controller.rb

auto_complete_for :case_manager, :name

case_manager is a different table…how do I identify this?

Craig


#16

On Jan 25, 2006, at 9:00 PM, Mike wrote:

For example, I have a (contrived) AnimalType filtering a list of many
Animals–more than can be presented comfortably in a dropdown box.

Instinct tells me you have a broken UI, if you’ve got more than a
‘comfortable’ amount of material in a widget. Pagable grids are
perhaps a hack to deal with this.

Therefore I need to either redirect to another screen to select an
animal (returning after the selection is made), or show a dialog–
modal
or otherwise–to make the selection. The latter might be presented
as a
pagable grid, or perhaps as a grid with a textbox or dropdown to
filter
search results.

What about Ajax auto-completion returning results for which there are
less then n (with a suitably small value for n) completions? So,
you’d just type in the name of the animal …

No clumsy dropdown, no warts in the workflow and a single text input
widget. Feels a lot less broken to me.

Is there a convention for this in Rails? I looked at BaseCamp for
examples but only found small groups of lookup values handled
easily by
simple dropdowns.

Yes. That’s the convention, in some sense. Use small groups only, or
don’t use dropdowns for such cases? I think Rails is about doing the
things that make sense and feel right. No insanity.

A third option would be DHTML magic to dynamically populate a magic
pseudo-window (the way some people handle searches), but I’m
looking for
a way to handle the lookup when this isn’t an option.

I assume you’re thinking about having Javascript turned off, or text
mode browsing?

I would include an unobtrusive, nearby link to a alphabetically
sorted list of the alternatives. Between people using oddball
configurations that make my life more difficult and making their
lives more difficult, I’d choose them jumping through the hoops.

They can always use the find function. ‘Syntactic sugar’ would be to
maybe put that list into a popup window. By clicking on a link in
that popup (or full blown page) you select your animal of choice …

Don’t over-design for the corner cases. Just handle them …

– G.


#17

On 1/26/06, Mike removed_email_address@domain.invalid wrote:

Strange that this isn’t addressed by the framework. Has no one
encountered a non-trivial lookup yet? :wink:

These things are going to be application-specific, I don’t really see
this as being something Rails should address.
In my opinion, the most Rails-like ways to do this are:

  1. RJS templates that replace the “Select a species” link with a grid,
    and then replace it when a selection is made.
  2. A helper that returns the required DHTML to build your selection
    popup.

Either way, the customer/user/etc is probably going to want a specific
behavior, look-and-feel, etc. Just as with user authentication, I
don’t see how Rails could implement this and satisfy a decent
percentage of the developers.

Myself, I’ve done this with option 1 above (well, I did it before RJS
templates, so it used my own Ajax template library hackjob), and it
worked well. I think RJS is as close as we’re likely to come to a
standardized method for doing this. Anything more fleshed-out would
tread into Components territory.

–Wilson.


#18

Strange that this isn’t addressed by the framework. Has no one
encountered a non-trivial lookup yet? :wink: