开发者

Rails 3 - How to use autocomplete with a few db fields as sources?

Hey guys!

Details

I'm using the rails3-jquery-autocomplete gem (https://github.com/crowd开发者_运维百科int/rails3-jquery-autocomplete) for my search feature.

To realize the search, I'm using meta-search gem.

Problem

My request is to realize a search on a radio model which contains few columns. Meta-search is able to do that but autocomplete gem seems to be currently limited to autocomplete from only one field at a time. I would like that the autocomplete will take as sources every field of the radio model.

Moreover, there are a few foreign keys in the radio model and I would like to get their corresponding values.

Questions

Did you have such a problem or do you know how to face with it?

I looked around over all the questions in stackoveflow, and tried to use the commits of that guy who implemented himself that feature -> Issue #15 on the gem github page

I would need maybe another gem or way to implement this autocomplete, so if you have any idea, don't hesitate to let me know.

Thx


There are several options, how to do things like that. One of them is to get some js autocomplete script, that can use external xml/json data source. I like (and use) this one - http://www.codeproject.com/KB/scripting/AutoSuggestControl.aspx

Using it, what you need to do is to implement some action for some controller, that will query all data sources you want with "LIKE"-style query (getting all words that start with or contain a parameter given) and return it all in way like this:

format.xml  { response.headers["Content-Type"] = "application/xml; charset=utf-8";
              render :inline => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
                                "<listdata>#{@tags.join('|')}</listdata>" };

@tags here is an array of all keywords you got with your query (or series of queries).

Your template will have a code like this:

<script>
  var obj = new autosuggest("someid", "", "http://mysite.org/tags.xml?term=");
</script>

Where "someid" is an "id" attribute of your search input, "/suggest.xml" is a path to your controller/action and "term" is your parameter for that action. I hope this could help you.


Finally, I used the fork of some guy who implemented interesting stuff. But that wasn't working properly so I forked his fork and I modified to be able to use it.

If you need help to autocomplete from few fields and tables on rails 3, ruby 1.8.7 then you can give a try to my fork of the rails3-jquery-autocomplete gem => https://github.com/dbachet/rails3-jquery-autocomplete

There is no support for mongoid, and options were not tested.

Thx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜