Forum: Ruby on Rails Lookup pattern in Ruby

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.
17a97e09a1b23bc733ae22880a961b59?d=identicon&s=25 Mike (Guest)
on 2006-01-25 22:00
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?
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-25 22:23
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
17a97e09a1b23bc733ae22880a961b59?d=identicon&s=25 Mike (Guest)
on 2006-01-25 23:09
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. ;)

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

Kevin Olbrich 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-26 03:21
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. ;)
>
> [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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-26 03:40
(Received via mailing list)
On Thu, 2006-01-26 at 03:21 +0100, Kevin Olbrich 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
28978f134cf63158fc5fc3f7e03b6768?d=identicon&s=25 James Ho (Guest)
on 2006-01-26 03:58
(Received via mailing list)
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 method `root' 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:in
`parse!'
         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:in `require'
         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
28978f134cf63158fc5fc3f7e03b6768?d=identicon&s=25 James Ho (Guest)
on 2006-01-26 04:13
(Received via mailing list)
Doh! I had not setup my environment.rb, oops!
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-26 04:23
Craig White wrote:
> On Thu, 2006-01-26 at 03:21 +0100, Kevin Olbrich 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-au...

http://www.slash7.com/articles/2005/08/13/ajaxarif...

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

_Kevin
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-26 05:04
(Received via mailing list)
On Thu, 2006-01-26 at 04:23 +0100, Kevin Olbrich wrote:
> > evidently I am not trying to accomplish it in a popular way.
>
> And page 388-394 or so in Agile Web Development 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
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-01-26 06:11
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-26 06:26
(Received via mailing list)
On Thu, 2006-01-26 at 04:23 +0100, Kevin Olbrich wrote:
> > evidently I am not trying to accomplish it in a popular way.
>
> And page 388-394 or so in Agile Web Development 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
<html>
  <%= javascript_include_tag "prototype" %>
  <head>

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)
);
28978f134cf63158fc5fc3f7e03b6768?d=identicon&s=25 James Ho (Guest)
on 2006-01-26 06:44
(Received via mailing list)
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-26 06:46
Craig White 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-26 07:47
(Received via mailing list)
On Thu, 2006-01-26 at 06:46 +0100, Kevin Olbrich wrote:
> Craig White 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-26 08:26
(Received via mailing list)
On Wed, 2006-01-25 at 22:25 -0700, Craig White wrote:
> > > I am struggling with this issue and haven't gotten any replies so
> > http://www.slash7.com/articles/2005/08/13/ajaxarif...
> 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
4ba4bb954bcfec077d9708b540558926?d=identicon&s=25 Guido Sohne (Guest)
on 2006-01-26 13:28
(Received via mailing list)
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.
17a97e09a1b23bc733ae22880a961b59?d=identicon&s=25 Mike (Guest)
on 2006-01-26 16:53
Strange that this isn't addressed by the framework. Has no one
encountered a non-trivial lookup yet? ;)
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-26 21:17
(Received via mailing list)
On 1/26/06, Mike <no@spam.com> wrote:
> Strange that this isn't addressed by the framework. Has no one
> encountered a non-trivial lookup yet? ;)
>

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.
This topic is locked and can not be replied to.