Mike Haslam Mike Haslam - 1 year ago 73
Ruby Question

Ruby how to make search filters case insensitive

I am building an app that has search fields how can I make search filters in my app case insensitive. Can I accomplish this in the following block of code:

<div class="filter">
<%= form_tag posts_path, method: :get do %>
<%= label_tag :bedrooms %>
<%= text_field_tag :bedrooms, params[:bedrooms] %>

<%= label_tag :bathrooms %>
<%= text_field_tag :bathrooms, params[:bathrooms] %>

<%= label_tag :neighborhood %>
<%= text_field_tag :neighborhood, params[:neighborhood] %>

<%= submit_tag "Search", name: nil %>
<% end %>

Here is relevant code from controller

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def home

def index
@posts = Post.all.paginate(:page => params[:page], :per_page => 15)
@posts = @posts.where(bedrooms: params["bedrooms"])if
@posts = @posts.where(bathrooms: params["bathrooms"])if
@posts = @posts.where(neighborhood: params["neighborhood"])if

Answer Source
posts = Post.find(:conditions => ["lower(bedroom) = ?", params[:bedrooms].downcase])

you can use lower and downcase to achieve that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download