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

  • Home
  • SEARCH
  • 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 8778659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:35:14+00:00 2026-06-13T19:35:14+00:00

I have implemented a simple search functionality for my rails 3 blog application. I

  • 0

I have implemented a simple search functionality for my rails 3 blog application. I want to validate it, in such a way, that with non-matching keywords, or when the search text field is blank, & when user clicks search button, it should display a message saying “your search criteria is invalid. Please try using valid keywords”

My Code is as follows :

In Post Model,

class Post < ActiveRecord::Base
attr_accessible :title, :body
validates_presence_of :search
validates :title, :presence => true, :uniqueness => true
validates :body, :presence => true, :uniqueness => true
  def self.search(search)
    if search
      where("title LIKE ? OR body LIKE ?","%#{search.strip}%","%#{search.strip}%")
    else
      scoped
    end
  end
end

In Post Controller,

 class PostsController < ApplicationController
  def index    
   @posts=Post.includes(:comments).search(params[:search])
   .paginate(per_page:2,page:params[:page]).order("created_at DESC")
  end
end

In Posts/index.html.erb (Views)

<div class = "search">
 <span>
  <%= form_tag(posts_path, :method => :get, :validate => true) do %>
    <p>
    <%= text_field_tag (:search), params[:search] %>
    <%= submit_tag 'Search' %>
  </br>
    <% if params[:search].blank? %>
    <%= flash[:error] = "Sorry... Your Search criteria didnt match. 
     Please try using  different keyword." %>
    <% else %>
    </p>
  <% end %>  
  </p>
  <% end %>
 </span>
</div>
  • 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-13T19:35:15+00:00Added an answer on June 13, 2026 at 7:35 pm

    Use ActiveModel for a tableless model with validations – maybe a model like PostSearch, on which you could add validations like you would for any other model.

    the model:

    class PostSearch
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming
    
      attr_accessor :input
    
      validates_presence_of :input
      validates_length_of :input, :maximum => 500
    
    end
    

    and your form:

    <%= form_for PostSearch.new(), :url=>posts_path, :method=>:get, :validate=>true do |f| %>
      <p>
        <%= f.label :input %><br />
        <%= f.text_field :input %>
      </p>
      <p><%= f.submit "Search" %></p>
    <% end %>
    

    Pair it with client side validations gem a good user experience.

    Info on ActiveModel:

    http://railscasts.com/episodes/219-active-model

    Railscast source code:

    https://github.com/ryanb/railscasts-episodes/tree/master/episode-219/

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

Sidebar

Related Questions

I have a simple rails search implemented in my app. At the time I
In our desktop application, we have implemented a simple search engine using an inverted
I have implemented a simple search function that highlights a row in a DataGrid
I have implemented a simple entity ejb with a @version annotation. I expect that
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I need to implement some search functionality within a Rails application. Most of the
I have a simple android application which does search. I have a custom popup
I have implemented a simple search on my website, the idea is choose what
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
I have implemented a simple linux shell in c. Now, I am adding some

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.