0

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
Dan
  • 139
  • 1
  • 6
  • 1
    What does "doesn't work" mean? you can be more specific: the url doesn't change? content doesn't change? the results are shown empty? what's shown on the server when you do the request? do you see any error? check the browser's inspector Network tab and see if the response of the request makes sense. Also, your search form has two fields named `search` and `location`and your search action uses a param with name `term` but the actual Article.search receives the params and uses the right keys (are those keys inside a `term` key`?) do you have a view for the search action? – arieljuod Nov 10 '18 at 04:33
  • Nothing at all happens. The server doesn't show any progress at all, it's almost as if I did not press the search button, but if I refresh the page....it works. I just realize though that when i hit refresh and search...it does work, and if i search again on the page it does work...and keeps working. It appears that it is only not working initially when i go to it. Now i do have the search bar load up at the top of every page.....maybe that is related to my problem? Once I refresh and search and it goes to the results...i can keep filling out the form and searching.... – Dan Nov 11 '18 at 06:10
  • I'm not sure, but it looks like a case for turbolinks messing things up, try disabling it jsut to be sure. Also, you didn't clarify the names conflict, you fetch `params[:term]` on the action but the form doesn't have the `term` key. I would also try triggering the submit manually from the console with `document.querySelector('.search-form form').submit()` and see what happens. – arieljuod Nov 11 '18 at 15:42

1 Answers1

0

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.

Dan
  • 139
  • 1
  • 6