Could not find Listing with 'id' = all, Search Form

So this is very strange. I followed this railscast http://railscasts.com/episodes/37-simple-search-form and after I implemented everything that looked like this

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 

I get the following error: Could not find Listing with 'id' = all I understand that the search methods look immediately for the identifier. However, I do not know how I need to configure it so that it looks through all my lists. find_by_all, of course, does not work.

I hope someone can help

Thank you

+5
source share
1 answer

what about modify listing.rb search method?

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

A plus..

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 
+6
source

Source: https://habr.com/ru/post/1202290/


All Articles