除非我刷新页面,否则搜索栏将不起作用

时间:2018-11-10 02:57:15

标签: ruby-on-rails google-maps ruby-on-rails-5 geokit

我遇到一个问题,我有一个搜索栏,它通过名称和位置搜索数据库。这确实有效。但是,实际上我必须大部分时间刷新页面才能正常工作。我尝试将其放置在另一页中,但似乎问题仍然存在。有什么想法吗?

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>

主页

<% 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 %>

搜索表单

<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>

搜索控制器

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

主要搜索功能

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 个答案:

答案 0 :(得分:0)

ArielJuod是对的。

我从我的application.js,gemfile和布局/应用程序中删除了Turbolinks。但是我确实在布局/应用程序文件中包括了这一行:

~/.ipython/profile_default/startup

现在,搜索栏可以立即工作,并且页面不必“刷新”即可工作。谢谢。