Search bar does not work unless i Refresh the page

770 views Asked by At

I have an issue where I have a search bar that searches my database via names and location. This does work. However, I have to actually refresh the page most of the time in order for it to work. I've tried placing it in another page, but it seems as if the issue carries over. Any ideas?

Application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Rumordom</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    <script src="//maps.google.com/maps/api/js?key=AIzaSyCyWj7qH1UB7GHF5HAi6OsphBRePFnO0FcwlyTA"></script>
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>  
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script>
<script src='//underscorejs.org/underscore-min.js' type='text/javascript'></script>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <%= render 'search/form' %>
    <div class="container">
      <% flash.each do |message_type, message| %>
        <%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>

  </body>
</html>

Main Home page

<% if logged_in? %>
  <div class="row">
    <aside class="col-md-4">
      <section class="user_info">
        <%= render 'shared/user_info' %>
      </section>
      <section class="stats">
        <%= render 'shared/stats' %>
        <%= render 'users/follow_form' if logged_in? %>
      </section>
      <%= link_to "Create a Business", businesses_new_path, class: "btn btn-lg btn-primary" %>
    </aside>
    <div class="col-md-8">
      <h3>Experience Feed(Businesses) <%= link_to "Users", user_feed_path, class: "btn btn-lg btn-primary" %></h3>

      <%= render 'shared/feed' %>

    </div>
  </div>
<% else %>
<div class="main_message">
<font color=#8c1aff font-weight: "bold" background-color: "purple">Rumordom</font>
  </div>
  <p>Welcome to Rumordom. This is a website where you can write about an experience you've had with a business. <strong>Every Experience Matters</strong> and someone should know about it. Click <%= link_to "HERE", about_path %> for more information about <font color=#8c1aff size="3em" font-weight: "bold">Rumordom</font>.</p>
  <div>
  <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
  <%= link_to "Write about an Experience", new_business_path, class: "btn btn-lg btn-primary" %>
  </div>
  <% end %>

Search form

<div class="span2">
    <div class="search-form" >
    <div class="before-buttons", style="font-size:1.2em;">
      <%= form_tag search_businesses_path, method: :get do |f| %>
<%= text_field_tag :search, nil, placeholder: "Search for A Business....write about your recent customer experience" %>
<div class="location_search">
<%= text_field_tag :location, nil, placeholder: "denver, miami, etc." %>
</div>
</div>
<div class="buttons">
<%= submit_tag "Search", class: "btn btn-md btn-primary" %>
</div>
      <% end %>
    </div>

Search Controller

class SearchController < ApplicationController
  def search
    if params[:term].nil?
      @businesses = []
    else
      @businesses = Article.search params[:term]
    end
  end
end

Main Search Function

def self.search(params)
    #businesses = Business.where(category_id: params[:category].to_i)
  businesses = Business.where("name like ? or city like ? or state like ?", "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%") if params[:search].present?
  businesses = businesses.within(20, :origin => "#{params[:location]}")
  businesses = businesses.sort_by{|x| x.distance_to("#{:location}")}
  businesses
  end
end
1

There are 1 answers

0
Dan On

ArielJuod was right.

I removed Turbolinks from my application.js, gemfile, and from layouts/application. However I did include this line in the layouts/application file:

<%= stylesheet_link_tag "application", media: "all" %>
    <%= javascript_include_tag "application" %>

Now the search bar works right away and the page does not have to be "Refreshed," for it to work. Thanks.