Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:09:22+00:00 2026-06-04T05:09:22+00:00

I generated a search_contoller and view for my rails app. My Controller: class SearchController

  • 0

I generated a search_contoller and view for my rails app.

My Controller:

class SearchController < ApplicationController

  def index
    if params[:commit] == "Search"
      # Great, now lets figure out what sort of query we're after
      @searchQuery = params[:q]
      @resultsReceived = true
      if 
         @sqlResults = Product.where("title like ? OR categories like ?", "%" + params[:q] + "%", "%" + params[:q] + "%").order("published DESC")
      end
    else
      @resultsReceived = false
      @sqlResults = []
    end

    # Render the page when we're done.
    respond_to do |format|
      format.html
    end
  end
end

My View

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

<% if @resultsReceived == true %>
<b><%= @sqlResults.length %> Results Found For: </b><%= @searchQuery %>
<br>
<table>
  <tr>
    <th>Publish</th>
    <th>Product</th>
    <th></th>
  </tr>
<% @sqlResults.each do |product| %>
 <tr class="<%= cycle('oddrow', 'evenrow') %>">
      <td><%= check_box_tag "product_ids[]", product.id, product.published, {:id => product.id, :class => "publish_checkbox"} %></td>
      <td><%= link_to 'Edit', edit_product_path(product) %></td>
      <td><%= link_to 'Destroy', product, confirm: 'Are you sure you want to KILL this product?', method: :delete %></td>   

      <td>
        <div>
          <b>Title:</b> <%= product.title %>
        </div>

        <div class="float_left prod_image">
            <a href='<%= product.url %>' target="_blank"><img src='<%= product.image_url %>' class='product_image'></img></a>
        </div>
        <div class="float_left prod_description">
          <div><b>Product is Published: </b> <%= product.published %></div>
          <div><b>Category Names:</b> <%= product.category_names %></div>
          <div><b>Categories:</b> <%= product.categories %></div>
          <div><b>Rating:</b> <%= product.rating %></div>
          <div><b>Wow:</b> <%= product.is_curated %></div>
          <div><b>Age:</b> <%= product.pr_attributes['ag_type'] %></div>
          <div><b>Gender:</b> <%= product.pr_attributes['gender_type'] %></div>
          <div>
            <b>Description:</b>
            <% if product.long_descr.length < 200 %>
              <%= product.long_descr %>
            <% else %>
              <span id="short_desc_<%= product.id %>"><%= product.long_descr[0..199] %>...</span>
              <span class="long_description", id="long_desc_<%= product.id %>"><%= product.long_descr  %> </span><a id="<%= product.id %>" class="toggle">toggle</a>
          <% end %>
          </div>
      <div><b>Backfill Male:</b> <%= product.backfill_text['male'] %></div>
      <div><b>Backfill Female:</b> <%= product.backfill_text['female'] %></div>
      <div><b>Backfill Unisex:</b> <%= product.backfill_text['unisex'] %></div>
      <div><b>MP Seller Name:</b><%= product.mp_seller_name %></div>
          <div><b>Price:</b> Current: $<%= product.curr_item_price %> / Base: $<%= product.base_item_price %></div>
      <div><b>ID STR:</b> <%= product.id_str %></div>
      <div><b>Stock:</b> <%= product.stock_status %></div>
      <div><b>Last Updated by <%= product.updated_by_name %> at <%= product.updated_at %></b></div>
        </div>
 </tr>

<% end %>
<% end %>

I need the search to be more specific, and would like to break up the sql statement, so I can search for other additional attributes. For example, I would like search for either TITLE (and get results that match only the string entered by the user) or search by GENDER (and get back only the gender I user is searching for). How can I do this? Should I create new form_tags for each type of search I want to perform? Is it possible to have just one search box in the view, and have the user select between radio buttons as to what they want to search for?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:09:23+00:00Added an answer on June 4, 2026 at 5:09 am

    With MySQL, your options are pretty limited to self constructing the LIKE clauses. Most Rails apps on MySQL will use other search options, like SOLr (sunspot), ElasticSearch, or Sphinx.

    On PostgreSQL, you have many more options to use the built in full text search capabilities, like Texticle.

    A lot of links to the above: https://www.ruby-toolbox.com/categories/rails_search

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can search engines such as Google index JavaScript generated web pages? When you right
I am building a toy Rails app. I generated a scaffold for my Post
I generated a controller called Search. later I built a model called search as
I have a grid that is dynamically generated based on search criteria. I render
I generated some of my C# code with an external tool. Each generated class
I generated .class files by the following command: javac -cp \directoryName\external.jar myPackageDirectory\First.java myPackageDirectory\Second.java I
The issue I'm having is related to multiple models in a single controller/view in
I have an ajax generated search results page which shows all results just fine.
I have a URL: Shipment/Search/{searchType}/{searchValue} and a controller action: // ShipmentSearchType is an enum
I have an app with Photos. Users can view photos directly from a link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.