简体   繁体   English

找不到带有'id'= all,Search Form的列表

[英]Couldn't find Listing with 'id'=all, Search Form

So this is very weird. 所以这很奇怪。 I followed this railscast http://railscasts.com/episodes/37-simple-search-form and after I implemented everything it looked like this 我跟着这个railscast http://railscasts.com/episodes/37-simple-search-form ,在我实现了它之后看起来像这样

index.html.erb index.html.erb

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

listings_controller.rb 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 listing.rb

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

I get the following error: Couldn't find Listing with 'id'=all I understand that the find methods looks right away for the id. 我收到以下错误:无法找到带有'id'的列表=我理解,find方法会立即查找id。 I however don't know how I need to set it so that it searches through all my listings. 然而,我不知道如何设置它以便搜索我的所有列表。 find_by_all of course does not work. find_by_all当然不起作用。

I hope anyone can help 我希望有人能提供帮助

Thank you so muc 谢谢你这么认真

how about modify listing.rb search method? 如何修改listing.rb 搜索方法?

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

Plus.. 加..

listings_controller.rb 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM