如何使用acts_as_taggable制作搜索表单

发布于 2024-12-05 00:31:59 字数 1320 浏览 0 评论 0原文

我有一个搜索表单,可以按标签搜索图像。该表单有点工作,它将参数发送到 /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

I have a search form to search for images by their tags. The form kinda works, it sends the parameters to the /search_results page but it sends as this:

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

And here is my form:

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

and and my route/controller:

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

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

But I never get any results.

Rails console shows this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2024-12-12 00:31:59

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

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

I removed the % surrounding my params and that seemed to do the trick:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文