我在我的应用程序中构建了一个功能,允许用户搜索可以搜索图像描述的图像,上传它的成员和图像标记但是我有这个奇怪的问题,如果我搜索名称'jason'它会中断,但像'jesse'这样的名字就可以了。
这是我的控制器
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
如果我搜索'jason'我的应用程序中断,给我一个'未定义的方法'错误,并且控制台显示一堆未显示的东西,如果我搜索说'jesse'。我不完全确定如何说出来,但这就是我的意思。
搜索 - 杰森
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 %>
答案 0 :(得分:5)
“|
vs ||
”问题不是您的问题。这样做:
Tattoo.where(...) | Tattoo.tagged_with(...) | Member.where(...)
将生成一个数组,它是三个输入结果的并集。切换到||
:
Tattoo.where(...) || Tattoo.tagged_with(...) || Member.where(...)
只会向您Tattoo.where(...)
提供,因为该查询永远不会是false
或nil
,||
会在出现非假结果后立即短路; Tattoo.where(...)
可能是空的,但空洞和虚假是不同的事情。
你的问题是你在一个数组中混合了两个不同的东西:来自前两个查询的Tattoo
个实例和来自上一个查询的Member
个实例。然后,您的ERB会假定@tattoos
中的所有内容都是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
并智能化您的视图逻辑,以便能够同时显示Tattoo
和Member
个实例。
答案 1 :(得分: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)
也许这可能是问题的一部分?
答案 2 :(得分: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 %>