2

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?

2
  • 1
    If you want to use the same term for both ?s, you need to pass @searchcriteria twice; otherwise it thinks you forgot one. Commented Oct 19, 2016 at 1:19
  • @philomory I don't know how that passed over my head. Thanks! Commented Oct 19, 2016 at 1:21

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

the productlist statement should look like the following:

@productlist = Product.where("productname like ? OR description like ?", @searchcriteria, @searchcriteria)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.