English 中文(简体)
奇怪的控制器行为 涉及一些参数 打破我的应用程序
原标题:Strange controller behavior involving where some params break my app

我在我的应用程序中建了一个功能, 让用户可以搜索图像, 搜索图像描述、 上传者、 以及图像标签, 但我有一个奇怪的问题,

这是我的控制器

  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 %>
最佳回答

我注意到一件事,就是您正在使用 而不是

也许这是问题的一部分?

问题回答

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

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

将产生三个输入结果的组合。 切换到 < code_ / code> :

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

只会给您 Tattoo. where (...) , 因为查询永远不会是 false nil , 而只要有非虚假的结果, 就会短路; tattoo. where (...) 可能是空的,但空的和假的却不一样。

您的问题是您将两个不同的东西重新混合在一个阵列中 : Tattoo 实例来自前两个查询,而 成员 实例来自最后一个查询。 然后您的 ERB 假设所有 tattoos 中的东西都是 Tattoo (或者对 成员 做出回应的其他东西):

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

如果您的 member. where (...) find anything, 那么您将至少有一个 member 实例在 中, 没有错误信息所示的 member# member 方法:

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

停止在 tattoos 中混合两个不同的类, 您的问题应该消失。 或者您可以将其重新命名为 matche , 并精通您的观点逻辑, 以便能够同时显示 Tattoo member 实例 。

我还要指出,你应该将这一逻辑推入模型,这样可以更容易地进行测试。

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




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...