ruby-on-rails – 找不到带有’id’= all,Search Form的列表

所以这很奇怪.
我跟着这个railscast http://railscasts.com/episodes/37-simple-search-form
在我实现了一切后,它看起来像这样

index.html.erb

<%= form_tag findjobs_path,:method => 'get' do %>
      <p>
      <%= text_field_tag :search %>
      <%= submit_tag "search" %>
       </p>
<% end %>

listings_controller.rb

def index
    @listings = Listing.all
    @listings = Listing.paginate(:page => params[:page],:per_page => 10)
    @user = User.find_by_name(params[:name])
    @listing = Listing.find_by_id(params[:id])
    @categories = Category.all
    @listings = Listing.search(params[:search])
    end
end

listing.rb

def self.search(search)
  if search
    find(:all,:conditions => ['name LIKE ?',"%#{search}%"])
  else
    find(:all)
  end
end

我收到以下错误:找不到’id’= all的列表
我知道find方法可以立即查找id.
然而,我不知道如何设置它以便搜索我的所有列表.
find_by_all当然不起作用.

我希望有人能提供帮助

谢谢你这么认真

解决方法

如何修改listing.rb搜索方法

def self.search(search)
  if search
    self.where("name like ?","%#{search}%")
  else
    self.all
  end
end

加..

listings_controller.rb

def index
  @listings = Listing.all 
  # Patching all Listing

  @listing = Listing.where(id: params[:id]) if params[:id].present?
  # Find By Id (For pagination,the 'where' statement result is Listing ActiveRecord::Relationship )
  @listings = @listings.search(params[:search]) if params[:search].present?
  # Search using Keyword
  @listings = @listings.paginate(:page => params[:page],:per_page => 10)
  # Pagination
  @user = User.find_by_name(params[:name]) if params[:name].present?
  # Find User using name column
  @categories = Category.all

end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...