---
title: "Search field without gems"
author: "Yaroslav Shmarov"
date: "2021-04-05"
canonical_url: "https://blog.superails.com/ruby-on-rails-search-field-without-gems"
markdown_url: "https://blog.superails.com/ruby-on-rails-search-field-without-gems.md"
description: "find records that contain text"
tags: ["ruby","rails","ruby-on-rails","search"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
---

# Search field without gems

### MISSION: field to search for `post.title` that **contains** characters

![search-field.png](https://blog.superails.com/assets/ruby-on-rails-search-field-without-gems/search-field.png)

posts_controller.rb
```ruby
  def index
    if params[:title]
      @posts = Post.where('title ILIKE ?', "%#{params[:title]}%").order(created_at: :desc) #case-insensitive
    else
      @posts = Post.all.order(created_at: :desc)
    end
  end
```

any view (posts/index.html.haml or in a **bootstrap** navbar)

```ruby
.form-inline.my-2.my-lg-0
  = form_tag(courses_path, method: :get) do
    .input-group
      = text_field_tag :title, params[:title], autocomplete: 'off', placeholder: "Find a course", class: 'form-control-sm'
      %span.input-group-append
        %button.btn.btn-primary.btn-sm{:type => "submit"}
          %span.fa.fa-search{"aria-hidden" => "true"}
```

.html.erb without bootstrap

```ruby
<%= form_tag(posts_path, method: :get) do %>
  <%= text_field_tag :title, params[:title], autocomplete: 'off', placeholder: "post title" %>
  <%= submit_tag "Search" %>
<% end %>
```
