0

我在我的应用程序中构建了一个功能,允许用户搜索将搜索图像描述、上传图像的成员和图像标签的图像,但我有一个奇怪的问题,如果我搜索名称“jason”,它会中断,但是像“杰西”这样的名字很好用。

这是我的控制器

  def search_results
    @tattoos = Tattoo.where("description LIKE ?", "%#{params[:search]}%") | Tattoo.tagged_with("#{params[:search]}", :any => true ) | Member.where("membername LIKE ?", "%#{params[:search]}%").order("created_at DESC").page(params[:page]).per(10)
  end

如果我搜索“杰森”,我的应用程序会中断,给我一个“未定义的方法”错误,如果我搜索“杰西”,控制台会显示一堆未显示的内容。我不完全确定如何表达,但这就是我的意思。

搜索 - 杰森

    Started GET "/search_results?utf8=%E2%9C%93&search=jason&commit=search" for 127.0.0.1 at 2012-05-23 20:48:26 -0400
  Processing by IndexController#search_results as HTML
  Parameters: {"utf8"=>"✓", "search"=>"jason", "commit"=>"search"}
  SQL (0.7ms)  SHOW TABLES
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT `tags`.* FROM `tags` WHERE (name LIKE 'jason')
  Tattoo Load (0.2ms)  SELECT `tattoos`.* FROM `tattoos` WHERE (description LIKE '%jason%')
  Tattoo Load (0.2ms)  SELECT `tattoos`.* FROM `tattoos` WHERE (1 = 0)
  SQL (0.7ms)  SHOW TABLES
  SQL (0.5ms)  SHOW TABLES
  Member Load (0.3ms)  SELECT `members`.* FROM `members` WHERE (membername LIKE '%jason%') ORDER BY fullname asc, created_at DESC LIMIT 10 OFFSET 0
  Member Load (0.3ms)  SELECT `members`.* FROM `members` WHERE `members`.`id` = 7 ORDER BY fullname asc LIMIT 1
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 43) AND (`taggings`.taggable_type = 'Tattoo')) AND (taggings.context = 'styles' AND taggings.tagger_id IS NULL)
Rendered shared/_image_roll.html.erb (27.5ms)
Rendered index/search_results.html.erb within layouts/application (29.3ms)
Completed 500 Internal Server Error in 523ms
  Processing by ErrorsController#internal_server_error as HTML
  Parameters: {"utf8"=>"✓", "search"=>"jason", "commit"=>"search"}
  SQL (0.7ms)  SHOW TABLES
  SQL (0.5ms)  SHOW TABLES
Rendered shared/_register.html.erb (483.4ms)
  Member Load (0.2ms)  SELECT `members`.* FROM `members` WHERE `members`.`id` = 7 ORDER BY fullname asc LIMIT 1
  MemberRole Load (0.2ms)  SELECT `member_roles`.* FROM `member_roles` WHERE (`member_roles`.member_id = 7) LIMIT 1
  SQL (0.6ms)  SHOW TABLES
  SQL (0.4ms)  SELECT COUNT(*) FROM `tattoos` WHERE `tattoos`.`status` = 'pending'
  SQL (0.2ms)  SELECT COUNT(*) FROM `feedbacks` WHERE `feedbacks`.`approved` = 0
  SQL (0.1ms)  SELECT COUNT(*) FROM `tattoos` WHERE `tattoos`.`status` = 'reported'
Rendered shared/_navbar.html.erb (87.6ms)
Rendered shared/_login_form.html.erb (2.9ms)
Rendered errors/internal_server_error.html.erb within layouts/application (582.4ms)
Completed 500 Internal Server Error in 587ms (Views: 583.9ms | ActiveRecord: 3.5ms)

ActionView::Template::Error (undefined method `member' for #<Member:0x007f0b0ca077b0>):
    1:   <ol class="image_roll">
    2:     <% @tattoos.each do |t| %>
    3:  <li>
    4:         <% unless t.member.nil? %>
    5: 
    6:           <%= link_to image_tag(t.file.url(:small),:alt=>"#{strip_tags(t.description)}, #{t.style_list}, tattoos"), member_tattoo_path(t.member, t)%>
    7:         <% else %>
  app/views/shared/_image_roll.html.erb:4:in `block in _app_views_shared__image_roll_html_erb__3185787815883689760_69842755443480_3152220748446100192'
  app/views/shared/_image_roll.html.erb:2:in `each'
  app/views/shared/_image_roll.html.erb:2:in `_app_views_shared__image_roll_html_erb__3185787815883689760_69842755443480_3152220748446100192'
  app/views/index/search_results.html.erb:10:in `_app_views_index_search_results_html_erb___3497910042066847338_69842586729040__4333294961394575926'

搜索 - 杰西:

Started GET "/search_results?utf8=%E2%9C%93&search=jesse&commit=search" for 127.0.0.1 at 2012-05-23 20:48:36 -0400
  Processing by IndexController#search_results as HTML
  Parameters: {"utf8"=>"✓", "search"=>"jesse", "commit"=>"search"}
  ActsAsTaggableOn::Tag Load (0.6ms)  SELECT `tags`.* FROM `tags` WHERE (name LIKE 'jesse')
  Tattoo Load (0.7ms)  SELECT `tattoos`.* FROM `tattoos` WHERE (description LIKE '%jesse%')
  Tattoo Load (0.1ms)  SELECT `tattoos`.* FROM `tattoos` WHERE (1 = 0)
  Member Load (0.5ms)  SELECT `members`.* FROM `members` WHERE (membername LIKE '%jesse%') ORDER BY fullname asc, created_at DESC LIMIT 10 OFFSET 0
Rendered shared/_search.html.erb (0.8ms)
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT tags.*, taggings.tags_count AS count FROM `tags` JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM `taggings` INNER JOIN tattoos ON tattoos.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Tattoo' AND taggings.context = 'styles') AND (taggings.taggable_id IN(SELECT tattoos.id FROM `tattoos`)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id ORDER BY count DESC LIMIT 40
Rendered index/_tags_list.html.erb (19.7ms)
Rendered layouts/shared/_right_column_std.html.erb (22.3ms)
Rendered shared/_register.html.erb (5.7ms)
  Member Load (0.3ms)  SELECT `members`.* FROM `members` WHERE `members`.`id` = 7 ORDER BY fullname asc LIMIT 1
  MemberRole Load (0.1ms)  SELECT `member_roles`.* FROM `member_roles` WHERE (`member_roles`.member_id = 7) LIMIT 1
  SQL (0.2ms)  SELECT COUNT(*) FROM `tattoos` WHERE `tattoos`.`status` = 'pending'
  SQL (0.1ms)  SELECT COUNT(*) FROM `feedbacks` WHERE `feedbacks`.`approved` = 0
  SQL (0.1ms)  SELECT COUNT(*) FROM `tattoos` WHERE `tattoos`.`status` = 'reported'
Rendered shared/_navbar.html.erb (13.5ms)
Rendered shared/_login_form.html.erb (2.9ms)
Rendered index/search_results.html.erb within layouts/application (54.3ms)
Completed 200 OK in 64ms (Views: 57.9ms | ActiveRecord: 3.0ms)

和观点:

<% @tattoos.each do |t| %>
    <% unless t.member.nil? %>

      <%= link_to image_tag(t.file.url(:small),:alt=>"#{strip_tags(t.description)}, #{t.style_list}, tattoos"), member_tattoo_path(t.member, t)%>
    <% else %>
      <%= link_to image_tag(t.file.url(:small),:alt=>"#{strip_tags(t.description)}, #{t.style_list}, tattoos"), tattoo_path(t) %>
    <% end %>
<% end %>
4

3 回答 3

5

|vs ||”问题不是你的问题。这样做:

Tattoo.where(...) | Tattoo.tagged_with(...) | Member.where(...)

将产生一个数组,该数组是三个输入结果的并集。切换到||

Tattoo.where(...) || Tattoo.tagged_with(...) || Member.where(...)

只会给你,Tattoo.where(...)因为那个查询永远不会是falseornil并且||一旦它有非错误的结果就会短路;Tattoo.where(...)可能是空的,但空和假是不同的东西。

您的问题是您在一个数组中混合了两种不同的东西:Tattoo前两个查询的Member实例和最后一个查询的实例。然后,您的 ERB 假定其中的所有内容@tattoos都是 a Tattoo(或其他响应 的内容member):

<% @tattoos.each do |t| %>
  <% unless t.member.nil? %>

如果您Member.where(...)发现任何东西,那么您将至少有一个Member实例,@tattoos并且没有Member#member方法,如错误消息所示:

ActionView::Template::Error (undefined method `member' for #<Member:0x007f0b0ca077b0>):

停止混合两个不同的课程@tattoos,您的问题应该会消失。或者您可以将其重命名为,例如,@matches并智能您的视图逻辑,以便能够同时显示TattooMember实例。

于 2012-05-24T02:05:31.880 回答
0

我注意到的一件事是你正在使用|而不是||

@tattoos = Tattoo.where("description LIKE ?", "%#{params[:search]}%") | Tattoo.tagged_with("#{params[:search]}", :any => true ) | Member.where("membername LIKE ?", "%#{params[:search]}%").order("created_at DESC").page(params[:page]).per(10)

也许这可能是问题的一部分?

于 2012-05-24T01:41:56.900 回答
0

我还认为你应该将这个逻辑移到模型中,这样应该更容易测试。

# Controller
@search_results = Tattoo.results_for_search(params[:search])
@search_results << Member.results_for_search(params[:search])

# Tattoo Model
def results_for_search(search_term)
  results = where("description LIKE ?", search_term).all
  results << tagged_with(search_term, :any => true)
end

# Member Model
def results_for_search(search_term)
  where("membername LIKE ?", search_term).order("created_at DESC")
end

# Search Results View
<% @search_results.each do |result| %>
  <% if result.is_a?(Tattoo) %>
    ...
  <% elsif result.is_a?(Member) %>
    ...
  <% end %>
<% end %>
于 2012-05-24T14:10:32.067 回答