1

我看过一些 Railscast,我知道这很简单,但无法设置。

实际的 SEARCH 本身工作正常。

例如,如果我输入“我们”,它会返回所有带有“我们”的结果。很公平。

在我的listing.rb 文件中,我有

  def self.search(search)
    s =  "%#{search}%"
    if search
      find(:all, :conditions => ["comments LIKE ? or doctor LIKE ? or website LIKE ? or     
      url LIKE ? or date LIKE ?", s, s, s, s, s])
    else
      find(:all)
    end
  end

在我的 listings_controller 文件中,我有:

def index
  @listings = Listing.search(params[:search_text])
end

所以在其他部分我显然不想要 find(:all)

return none 或 find none 的编码等价物是什么?返回零?

如果您在表单本身中并且它是空白的并且您按回车键,它应该不返回任何内容并且应该显示“未找到结果”或类似的内容。

通知本身是否必须在控制器中,我的意思是我不能像渲染:text => 'No results found' 之类的吗?

或类似的东西

elsif
  search.blank? 
  render [:index], flash[:notice] = 'No Results Found'
end

我知道这很简单,但已经离开了编码游戏一段时间,而且对 RoR 来说总体上相对较新。

非常感谢任何输入。谢谢

4

2 回答 2

2

我不认为代码必须在控制器中

 def self.search(search)
    s =  "%#{search}%"
    if search
      find(:all, :conditions => ["comments LIKE ? or doctor LIKE ? or website LIKE ? or     
      url LIKE ? or date LIKE ?", s, s, s, s, s])
    else
      nil
    end
  end

并且在视野中

<% if @listings.present? %>
 //some code here 
<% else %>
  <p>No results found</p>
<% end %>
于 2012-10-10T15:02:29.057 回答
0

尝试根据您的代码进行更改

def self.search(search)
    s =  "%#{search}%"
    if search
      where("comments like (?) or doctor like (?)",s,s)
    else
     all
    end
  end
于 2012-10-10T14:58:05.010 回答