samedi 19 novembre 2016

Implementing Ruby on Rials search but getting "Couldn't find all Listings with 'id':"

I'm trying to implement a search function on my site following this rails cast. However it's not really working and it displays...

Couldn't find all Listings with 'id': (all, {:conditions=>["title LIKE ?", "%Example%"]}) (found 0 results, but was looking for 2)

I tried looking around for what is going on and found this question on the site, tried implementing the fixes for it since I suppose Rails Cast is using a very old version of rails. Though I am still getting the same thing.

listing_controller.rb search method

def search
    @listing = Listing.where(id: params[:id]) if params[:id].present?
    @listing = Listing.search(params[:search]) if params[:search].present?
end

listing.rb

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

index.html.erb

  <p>search shiplist</p>
  <%= form_tag search_listings_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
  <% end %>

Aucun commentaire:

Enregistrer un commentaire