我对 Ruby on Rails 相当陌生,将目录类型的网站作为个人项目来学习。
在我的应用程序中,我实现了地理编码器搜索,因此用户可以搜索他们附近的地点/位置,搜索效果很好。但是当我添加选项卡式菜单来分类可用的地方类型时,它破坏了搜索。
我想我知道它为什么坏了,但不知道如何修复它。选项卡显示@schools、@restaurants 和@businesses 变量,我怎样才能让选项卡显示搜索结果而不是那些变量?这些变量应该只在没有执行搜索时显示,但它们也会在搜索执行时显示。
这是我的代码:
地方控制器:
class PlacesController < ApplicationController
before_filter :authenticate_user!, except: [:index]
def index
if params[:search].present?
@places = Place.near(params[:search], 50, :order => :distance)
else
@places = Place.all
end
@schools = Place.where("category = ?", "School")
@restaurants = Place.where("category = ?", "Restaurant")
@businesses = Place.where("category = ?", "Business")
end
地方索引.html.erb:
<% if user_signed_in? %>
<div class="row">
<h2>Newly Added Places</h2>
<%= form_tag places_path, :method => :get do %>
<%= text_field_tag :search, params[:search], placeholder: "Enter your zipcode or city", class: "search-query" %>
<%= submit_tag "Search Nearby", :name => nil, class: "btn"%>
<% end %>
<ul class="nav nav-tabs">
<li><a href="#tab1" data-toggle="tab">Schools</a></li>
<li><a href="#tab2" data-toggle="tab">Restaurants</a></li>
<li><a href="#tab3" data-toggle="tab">Businesses</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<%= render :partial => "places", :locals => {:places_container => @schools} %>
</div>
<div class="tab-pane" id="tab2">
<%= render :partial => "places", :locals => {:places_container => @restaurants} %>
</div>
<div class="tab-pane" id="tab3">
<%= render :partial => "places", :locals => {:places_container => @businesses} %>
</div>
</div>
</div>
<br />
<% else %>
<%= render 'static_pages/home' %>
<% end %>
地方部分(_places.html.erb):
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Address</th>
<th>State</th>
<th>City</th>
<th>Type</th>
<th>Website</th>
</tr>
</thead>
<% places_container.each do |place| %>
<tr>
<th><%= image_tag place.image(:medium)%></th>
<th><%= link_to place.name, place %></th>
<td><%= place.address %></td>
<td><%= place.state %></td>
<td><%= place.city %></td>
<td><%= place.category %></td>
<td><%= place.website %></td>
<% if current_user.admin? %>
<td><%= link_to 'Edit', edit_place_path(place) %></td>
<td><%= link_to 'Destroy', place, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>