Rails的auto_complete标签搜索过滤器 [英] Rails auto_complete tag search filter

查看:98
本文介绍了Rails的auto_complete标签搜索过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图实现轨auto_complete插件来帮助用户选择不主页上显示的标签,因为会有潜在的可显示数百个标签,只有一小部分。

Trying to implement the rails auto_complete plugin to help a user select tags that don't appear on the home page since there will be potentially hundreds of tags and only a small fraction can be displayed.

我的导航的工作方式是这样的:我开始与类别显示页面显示该类别中的所有文章。当用户点击一个标签,在这样的网址带来了一个变量过滤器:

The way my navigation works is like this: I start with a category show page that displays all articles within that category. When the user clicks a tag, that brings up a tag filter in the url like this:

http://localhost:3000/categories/Stories?tag=scary

这将显示在故事类具有标签的所有文章可怕的。

This will show all articles in the stories category that have the tag "scary."

由于吓人,是不是一个受欢迎的标签就不会在主页上显示出来,但如果你把它放到自动填充文本字段吓人会显示出来。我想提交标签来显示与上述相同的URL。

Since "scary" is not a popular tag it wouldn't show up on the home page, but if you put it into the autocomplete text field "scary" would show up. I'd like the submit tag to render the same url as above.

但我得到的东西有点不同:

But I'm getting something a little different:

http://localhost:3000/categories/ShortStories?tag[name]=scary

不幸的是,因为那个讨厌的[名]在那里偷偷这个过滤器将不会返回任何东西。

Unfortunately this filter won't return anything because that pesky [name] sneaks in there.

下面是我的控制器code的自动完成:

Here's my controller code for the autocomplete:

 class CategoriesController < ApplicationController
 auto_complete_for :tag, :name

和视图

<% form_tag category_path, :method => 'get' do %>
<%= text_field_with_auto_complete :tag, :name, { :size => 15 } %>
<%= submit_tag "Search All Tags", :name => nil %>
<% end %>

本:名称似乎是必要的,因为auto_complete需要指定列名,但我希望把它拿出来的网址,当我点击提交。任何想法?

The :name seems to be required because auto_complete needs to specify a column name but I want to take it out of the url when I hit submit. Any ideas?

推荐答案

一些搜索,我发现我一直在寻找的经过。 本博客文章是非常有益的让我在正确的道路上。

After some searching I found what I was looking for. This blog post was very helpful getting me on the right path.

这就是我得到了什么:

在控制器:

@search_tags = Tag.find_by_keyword(params[:tag])
	respond_to do |format|
	    format.html
	    format.js do
	      render :inline => "<%= auto_complete_result(@search_tags, 'name') %>"
	    end
	 end

在该视图:

<div id="search">
  <% form_tag(category_path(), {:method => :get, :class => "form"}) do %>    
    <%= text_field_with_auto_complete :tag, :name, 
        { :name => "tag", :size => 20 },
        {:method => :get, :url => category_path(:format => :js) } %>
    <%=submit_tag "Search All Tags", :name => nil%>
  <% end -%>

最后,我添加了一个方法,在供应商的tag.rb模型/插件/ acts_as_taggable_on_steroids / lib目录

Finally, I added a method to the tag.rb model in vendor/plugins/acts_as_taggable_on_steroids/lib

  def self.find_by_keyword(keyword)
      if keyword.present?
        keyword = "%#{keyword}%"
        @search_tags = Tag.all :conditions => ["name like ?", keyword], :order => "name asc", :limit => 10
      else
        @search_tags = Tag.all :limit => 10, :order => "name asc"
      end
  end

和它的作品!

这篇关于Rails的auto_complete标签搜索过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆