I am trying to add a search functionality to my ruby on rails application. The search works fine but how can I add a validation so it displays when the search is empty?
I have tried adding required: true
but that doesn't seem to do much.
index.html.erb:
<%= form_tag topics_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], required: true %>
<%= submit_tag "Search" %>
<% end %
topics_controller:
def index
@topics = Topic.search(params[:search]).paginate(:page => params[:page], :per_page => 5)
end
topics.rb
def self.search(search)
if search
where(["title LIKE ?","%#{search}%"])
else
all
end
I expect the output to be: 1. Search for particular topic 2. There is no topics within that field so a validation is displayed such as "no results found, please try again"