У меня проблема с поиском в моей базе данных по именам и местоположению. Это работает. Тем не менее, я должен обновлять страницу большую часть времени, чтобы она работала. Я пытался разместить его на другой странице, но кажется, что проблема переносится. Есть идеи?
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