Forum: Ruby on Rails Rails autocomplete

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.
2a41d040660b64d5682740ef1713281c?d=identicon&s=25 Any Dot (anydot)
on 2007-03-12 00:54
Hi,

A short question from newbie - hope wouldn't be a problem for you.

I am playing with autocomplete features in rails  -

In View:
<%= text_field_with_auto_complete :model, :attribute %>

In Controller:
auto_complete_for :model, :attribute
(found many examples in www)

So far, so gut. Autocomplete works. But I want the following - if I type
'm', only words starting with 'm' to appear.
I have the word - 'Belgium' - when I fill 'm' in the field, 'Belgium' is
fetched out, cause there is a 'm' in it.

Could someone explain me what is missing and probably give me some
directions?

Thanks in advance.
5b75d07b5adfc157376fe012845cb08a?d=identicon&s=25 Jamey Cribbs (Guest)
on 2007-03-12 02:51
(Received via mailing list)
Any Dot wrote:
> So far, so gut. Autocomplete works. But I want the following - if I type
> 'm', only words starting with 'm' to appear.
> I have the word - 'Belgium' - when I fill 'm' in the field, 'Belgium' is
> fetched out, cause there is a 'm' in it.
>

do this

"country LIKE ?%"

instead of this

"country LIKE %?%"

in your conditions clause.

Jamey
5b75d07b5adfc157376fe012845cb08a?d=identicon&s=25 Jamey Cribbs (Guest)
on 2007-03-12 03:09
(Received via mailing list)
Any Dot wrote:
> auto_complete_for :model, :attribute
> (found many examples in www)
>
> So far, so gut. Autocomplete works. But I want the following - if I type
> 'm', only words starting with 'm' to appear.
> I have the word - 'Belgium' - when I fill 'm' in the field, 'Belgium' is
> fetched out, cause there is a 'm' in it.
>
Sorry, should have read your question closer.

This might work:

auto_complete_for :model, :country, :conditions => [ "LOWER(country)
LIKE ?", params[:country_field].downcase + '%' ]

If this doesn't work, you might just have to define your own
auto_complete_country method (see page 547 in Agile), which is really
what auto_complete_for is doing for you anyway.

Jamey
2a41d040660b64d5682740ef1713281c?d=identicon&s=25 Any Dot (anydot)
on 2007-03-12 15:23
Thanks Jamey,

Actually, I wrote my own auto_complete method - it is working now.

Many thanks
This topic is locked and can not be replied to.