Frank Frank - 1 month ago 6
Ruby Question

Search for item name from multiple fields on Ruby on Rails

I'm trying to enable a search for an item, and then have that item listed if the search is completed. I can get it to work for both instances, but not at the same time. My code:

def search
@results=0
if !params[:searchinput].empty?
@results=1
@searchinput = params[:searchinput]
@searchcriteria="%#{params[:searchinput]}%"
@productlist = Product.where("productname like ? OR description like ?", @searchcriteria)
end
end


I get the following error:
enter image description here
Is the formatting with the productlist statement wrong?

Answer

The problem is you have to pass in two variables. If that is @searchcriteria then pass that in.

def search
    @results=0
        if !params[:searchinput].empty?
            @results=1
            @searchinput = params[:searchinput]
            @searchcriteria="%#{params[:searchinput]}%"
            @productlist = Product.where("productname like ? OR description like ?", @searchcriteria, @second_variable)
    end
end
Comments