I recently integrated an instant search feature into my application.
During testing on the local server, the functionality met my expectations:
- It filters the list as I type
- It is not case-sensitive
- It allows for resetting the search if I delete the input
However, when deployed to production, the behavior is erratic. Despite having the same keypress search functionality, several issues arise:
- The search is case-sensitive, unlike in development
- The resetting mechanism does not work as expected
- The search results seem inaccurate. For example, typing "z" does not filter out any items, whereas typing "Blogging" correctly narrows down the list
I am puzzled by this discrepancy. Why are the two environments behaving differently? And how can I resolve these issues? Any insights or code suggestions would be greatly appreciated.
Here is a snippet of my code:
archive.html.erb
<%= form_tag @post, :method => 'get', :id => "posts_search", class: "search_form squeeze form-inline" do %>
<p>
<%= text_field_tag :search, params[:search],
placeholder: "Search titles:", id: "search_field" %>
<%= submit_tag "Search", name: nil, class: "btn squeeze search" %>
</p>
<div id="list"><%= render 'search' %></div>
<% end %>
_search.html.erb
<ul class="blog_links">
<% @posts.first(@link_num).each do |p| %>
<li class="total_hover">
<%= p.name %>
</li>
<% end %>
</ul>
archive.js.erb
$("#list").html("<%= escape_javascript(render("search")) %>");
posts_controller.rb
def archive
@posts = Post.search(params[:search]).reverse
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
format.js
end
end
def search
@posts = Post.search(params[:search]).reverse
render json: { results: @posts }
end
post.rb
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
javascripts/posts.js.coffee
@search = ->
$.get $('#posts_search').attr("action"), $("#posts_search").serialize(), null, "script"
$ ->
$('#posts_search input').keypress -> search()
$('#posts_search').submit (e) ->
e.preventDefault()
search()
routes.rb
match '/search', to: 'posts#search'
match '/archive', to: 'posts#archive'
EDIT To provide some clues, I'm executing the same user behavior in both environments and posting the blogs. What I'll do is this:
- Load the page containing the search.
- Enter "Blgo"
- Delete the "go" and replace it with "og"
- Delete everything and search for "Insta"
development logs
...
heroku logs
...
Again, the search functionality works differently in production compared to development. Any suggestions on resolving this inconsistency are welcome.