简体   繁体   English

Rails auto_complete标记搜索过滤器

[英]Rails auto_complete tag search filter

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. 尝试实现rails auto_complete插件以帮助用户选择主页上未显示的标签,因为可能存在数百个标签,并且只能显示一小部分。

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." 这将显示故事类别中所有带有标签“ scary”的文章。

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. 由于“ scary”不是受欢迎的标签,因此不会显示在主页上,但是如果将其放入自动完成文本字段中,则会显示“ scary”。 I'd like the submit tag to render the same url as above. 我希望Submit标签呈现与上述相同的网址。

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. 不幸的是,此过滤器不会返回任何内容,因为讨厌的[name]会潜入其中。

Here's my controller code for the autocomplete: 这是自动完成的控制器代码:

 class CategoriesController < ApplicationController
 auto_complete_for :tag, :name 

And the view 和看法

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

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. :name似乎是必需的,因为auto_complete需要指定列名,但是我想在单击Submit时将其从URL中删除。 Any ideas? 有任何想法吗?

After some searching I found what I was looking for. 经过一番搜索,我找到了想要的东西。 This blog post was very helpful getting me on the right path. 这篇博客文章对帮助我找到正确的道路非常有帮助。

So this is what I got: 这就是我得到的:

In the controller: 在控制器中:

@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

In the view: 在视图中:

<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 -%>

Finally, I added a method to the tag.rb model in vendor/plugins/acts_as_taggable_on_steroids/lib 最后,我在vendor / plugins / acts_as_taggable_on_steroids / lib中的tag.rb模型中添加了一个方法

  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

And it works! 而且有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM