ruby-on-railsrubypg-search

View results of a search in index


What i'm trying to achieve is a product index page which shows:

(1) the result of my search if it matches a record in my database,

(2) all my products if the user input is blank (or if I simply visit the /products page), and

(3) a message which says the product hasn't been found if the product doesn't exist (is not in the database).

The code below returns me all the products if I insert a blank space in the search field or if I insert a string that doesn't match any record in the database, and a correct product if it's found it in the database.

<div class="container">
  <div class="row">
    <% if @products_search.present? %>
      <% @products_search.each do |product| %>
        <div class="col-sm-8 col-sm-offset-2">
          <%= product.name %><br>
          <%= product.description %><br>
          <%= humanized_money_with_symbol(product.price)%><br>
          <%= link_to "Show Product", product_path(product) %>
        </div>
      <% end %>

    <% elsif @products_search.blank? %>
      <% @products.each do |product| %>
        <div class="col-sm-8 col-sm-offset-2">
          <%= product.name %><br>
          <%= product.description %><br>
          <%= humanized_money_with_symbol(product.price)%><br>
          <%= link_to "Show Product", product_path(product) %>
        </div>
      <% end %>

    <% else %>
      <h1>No products found!</h1>
    <% end %>
  </div>
</div>


class ProductsController < ApplicationController

  def index
    @products_search = Product.search_by_name_and_description(params[:term])
    @products = Product.all
  end
end

class Product < ApplicationRecord
  has_and_belongs_to_many :orders
  belongs_to :category
  belongs_to :user

  monetize :price_cents

  include PgSearch
  pg_search_scope :search_by_name_and_description,
                  against: [ :name, :description ],
                  associated_against: {
                      category: [ :name ]
                  },
                  using: {
                      tsearch: { prefix: true }
                  }
end

    <%= form_tag products_path, method: :get do %>
  <%= text_field_tag 'term', params[:term], placeholder: "What are you looking for?", :required => true %>
  <%= submit_tag 'Search!' %>
<% end %>
<br>

Solution

  • With this code, i finally achieved what I was trying to do.

    <div class="container">
      <div class="row">
        <% if params[:term].present? %>
          <% if @products_search.present? %>
            <% @products_search.each do |product| %>
            <div class="col-sm-8 col-sm-offset-2">
              <%= product.name %><br>
              <%= product.description %><br>
              <%= humanized_money_with_symbol(product.price)%><br>
              <%= link_to "Show Product", product_path(product) %>
            </div>
            <%end%>
          <% else %>
            <h1>No products found!</h1>
          <%end%>
        <% else %>
          <% @products.each do |product| %>
          <div class="col-sm-8 col-sm-offset-2">
            <%= product.name %><br>
            <%= product.description %><br>
            <%= humanized_money_with_symbol(product.price)%><br>
            <%= link_to "Show Product", product_path(product) %>
          </div>
          <%end%>
          <%end%>
      </div>
    </div>