开发者

Rails - Using Autocomplete with the Scoped_search gem?

I'm using the scoped_search gem for basic search functionality on my site. Scoped search: http://github.com/wvanbergen/scoped_search

I'm using the most basic possible implementation of this gem. I'm searching on the name attribute in a user model only. On the frontend, in users/index, the search input is a text_field_tag.

<% form_tag users_path, :method => :get do %>  
    <%= text_field_tag :search, params[:search], :value => 'Search' %>
    <%= submit_tag "Go", :name => nil %>
<%end%>

In the user model, I have:

scoped_search :on => :name

I want to implement basic auto-complete functionality on the user model, name attribute. This would be the most basic use case if I was开发者_StackOverflow社区 searching in a form element of the user model, but the scoped_search part is throwing me off.

I was planning to use DHH's auto_complete plugin, but I'm open to using whatever tool helps me accomplish this. Auto-complete: http://github.com/rails/auto_complete

Thanks.


  • In the model you should replace the line:

    scoped_search :on => :name

with:

scoped_search :on => :name, :complete_value => true
  • In the view replace the line:

    <%= text_field_tag :search, params[:search], :value => 'Search' %>

with:

<%= auto_complete_field_tag_jquery(:search, params[:search], {},{}) %>
  • In the controller you should add the following method:

    def auto_complete_search

    @items = User.complete_for(params[:search])

    render :json => @items

    end


did you see the demo app at https://github.com/abenari/scoped_search_demo_app ?

Alternatively, you can see how I use it in my project - https://github.com/ohadlevy/foreman

Ohad

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜