【问题标题】:How to make a search form with acts_as_taggable如何使用acts_as_taggable 制作搜索表单
【发布时间】:2011-09-15 21:48:06
【问题描述】:

我有一个搜索表单,可以通过标签搜索图像。表单有点用,它将参数发送到 /search_results 页面,但它是这样发送的:

search_results?utf8=✓&search=squid%2C+color&x=0&y=0

这是我的表格:

<%= form_tag ("/search_results"), :method => "get", :class=>"search_form" do %>
  <%= text_field_tag ("search"), nil, :class => 'search_input',
    :onblur=>"if(this.value=='')this.setAttribute('class', 'search_input');",
    :onfocus=>"this.setAttribute('class', 'search_input_clear');"
 %>
  <%= image_submit_tag("search.png") %>
<% end %>

还有我的路线/控制器:

match "/search_results/" => "index#search_results", :via => :get, :as =>"search_results"
class IndexController < ApplicationController

def search_results
  @tattoos = Tattoo.tagged_with("%#{params[:search]}%")
end

但我从来没有得到任何结果。

Rails 控制台显示如下:

Parameters: {"utf8"=>"✓", "search"=>"color, animals", "x"=>"0", "y"=>"0"}
  SQL (0.5ms)  SHOW TABLES
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT `tags`.* FROM `tags` WHERE (name LIKE '\\%color' OR name LIKE 'animals\\%')
  SQL (0.1ms)  SELECT COUNT(*) FROM `tattoos` WHERE (1 = 0)
  Tattoo Load (0.3ms)  SELECT `tattoos`.* FROM `tattoos` WHERE (1 = 0) ORDER BY tattoos.created_at DESC

【问题讨论】:

  • 我还不确定问题是什么,虽然看起来您在操作中使用了参数“q”,但在表单中使用了“搜索”(奇怪的是在括号中?)。
  • 我试图从我刚刚发布的链接和 text_field_tag 上的 ruby​​ api 中拼凑这个搜索功能
  • 真正的问题是什么?似乎字段标签助手正在做它应该做的事情。
  • 看看链接的问题:stackoverflow.com/questions/5090682/…
  • 它没有传入搜索参数,它传递的内容如下:{"utf8"=>"✓", "search"=>"jesse smith, sleeve, color", "x"=> "14", "y"=>"10"},所以控制器找不到任何匹配的记录

标签: ruby-on-rails ruby-on-rails-3 search acts-as-taggable-on


【解决方案1】:

我删除了参数周围的 % ,这似乎可以解决问题:

定义搜索结果 @tattoos = Tattoo.tagged_with("#{params[:search]}") 结束

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多