simple rails search form not working

joey

I have a very simple, typical search form in rails. Input a string, and it checks a column for that model if any strings match it. A few weeks ago it was working just fine. I came back today, and suddenly doesn't work.

Here is my code. My search form:

<%= form_tag("/search/products", method: "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>

My Controller:

def search
 term = params[:q]
 puts "the term is #{term}"
 @resultats = Product.search_products(term)
end

In my model, the search_products method:

  def self.search_products(search_term)
    if search_term == ""
        return []
    else
        where("name LIKE ?", "%#{search_term}")
    end
  end

in the controller code, the puts "the term is #{term} prints the correct term every time. So I know that is being picked up correctly. just, when i search for something, it doesn't return the correct results. Here is a screenshot from my terminal:

enter image description here

"Les résultats sont" in the terminal means "the results are..." and then empty, because it returns nothing. What could be wrong here?

Shadow Radiance

Your code currently searches for LIKE '%term' (string must end in term)

You probably need to have it search for LIKE '%term%' (string must contain term)

Just add a %:

where("name LIKE ?", "%#{search_term}%")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Simple search not working on rails app

From Dev

Search Form In Rails Application Not Working

From Dev

form search not working in rails app

From Dev

Ruby on Rails - Simple Form autocomplete association search

From Dev

Rails : Simple search form, with keywords jambled

From Dev

Rails Simple_Form Association Not Working

From Dev

rails gem Simple_form installation is not working

From Dev

search form not working and gives error in rails app

From Dev

Rails 4, Bootstrap 3, simple_form - form styling not working

From Dev

Search function not working in footable when rendering partial form in rails 4

From Dev

PHP Simple Search Form

From Dev

Rails Simple Search

From Dev

Rails Simple Search

From Dev

Simple step form not working

From Dev

Simple form not working

From Dev

Rails Devise & Simple Form

From Dev

Rails with Simple Form - Styling

From Dev

Why won't my simple Rails form_tag search form provide results using the right method?

From Dev

Rails search form with no path

From Dev

Simple_form class form-horizontal with bootstrap 3 not working in rails 4

From Dev

Simple_form class form-horizontal with bootstrap 3 not working in rails 4

From Dev

HTML Form not working in rails

From Dev

Ruby simple search form error

From Dev

Ruby simple search form error

From Dev

PHP/MYSQLI simple search not working

From Dev

Simple search in jQuery not working properly

From Dev

Simple Search with Rails - Search Results on Separate Page

From Dev

Simple PHP submit form not working

From Dev

Simple PHP submit form not working