0

I'm fairly new to Ruby on Rails, making a directory type of website as a personal project to learn.

In my app, I implemented Geocoder search so the user can search for places/locations near them, the search worked perfect. But then when I added tabbed menus to categorize what type of places are available it broke the search.

I think I know why it's broken but not sure how to fix it. The tabs are showing the @schools, @restaurants, and @businesses variables, how can I get the tabs to show the search results and not those variables? Those variables should only show up when there is no search executed, but they also show up when the search is executed.

Here's my code:

Places Controller:

    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

Places Index.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 partial(_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>
Farooq
  • 1,925
  • 3
  • 15
  • 36

1 Answers1

1

Your tabbed results are ignoring the search parameter because you aren't using the @places relation built from the search parameter. To fix, change this code:

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

to:

def index
  if params[:search].present?
   @places = Place.near(params[:search], 50, :order => :distance)
  else
   @places = Place.scoped  # use 'scoped', not 'all', to avoid triggering the query immediately
  end

  @schools = @places.where("category = ?", "School")
  @restaurants = @places.where("category = ?", "Restaurant")
  @businesses = @places.where("category = ?", "Business")
end
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • @Farooq: Have you tried restarting the rails service? If that doesn't help, please post the SQL that is being generated both with and without a search param. – PinnyM Mar 11 '13 at 03:53
  • Yes I restarted the server. – Farooq Mar 11 '13 at 03:56
  • With search param: Started GET "/places?utf8=%E2%9C%93&search=20167" Processing by PlacesController#index as HTML Parameters: {"utf8"=>"✓", "search"=>"20167"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Place Load (0.1ms) SELECT "places".* FROM "places" WHERE (category = 'School') Rendered places/_places.html.erb (1.8ms) Place Load (0.1ms) SELECT "places".* FROM "places" WHERE (category = 'Restaurant') Rendered places/_places.html.erb (1.2ms) Place Load (0.1ms) SELECT "places".* FROM "places" WHERE (category = 'Business') – Farooq Mar 11 '13 at 04:01
  • Without search param: Started GET "/places?utf8=%E2%9C%93&search=" Processing by PlacesController#index as HTML Parameters: {"utf8"=>"✓", "search"=>""} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Place Load (0.2ms) SELECT "places".* FROM "places" WHERE (category = 'School') Rendered places/_places.html.erb (2.2ms) Place Load (0.2ms) SELECT "places".* FROM "places" WHERE (category = 'Restaurant') Rendered places/_places.html.erb (1.4ms) Place Load (0.1ms) SELECT "places".* FROM "places" WHERE (category = 'Business') – Farooq Mar 11 '13 at 04:02
  • @Farooq: can you post your `Place.near` scope/class method? Something here doesn't add up... – PinnyM Mar 11 '13 at 14:12
  • I did this in rails console: 1.9.3p194 :008 > Place.near("South Plainfield") Place Load (0.6ms) SELECT places.*, (69.09332411348201 * ABS(places.latitude - 40.5792701) * 0.7071067811865475) + (59.836573914187355 * ABS(places.longitude - -74.4115401) * 0.7071067811865475) AS distance, CASE WHEN (latitude >= 40.5792701 AND longitude >= -74.4115401) THEN 45.0 WHEN (latitude < 40.5792701 AND longitude >= -74.4115401) THEN 135.0 WHEN (latitude < 40.5792701 – Farooq Mar 11 '13 at 15:35
  • ...continued... AND longitude < -74.4115401) THEN 225.0 WHEN (latitude >= 40.5792701 AND longitude < -74.4115401) THEN 315.0 END AS bearing FROM "places" WHERE (latitude BETWEEN 40.289806533778304 AND 40.8687336662217 AND longitude BETWEEN -74.79266058521392 AND -74.03041961478607) ORDER BY distance – Farooq Mar 11 '13 at 15:37
  • ...continued... => [#] – Farooq Mar 11 '13 at 15:37
  • @Farooq: I meant the code for this method :) - in any event it apparently uses the AR query interface so this is not the issue. Are you sure you replaced `Place.where...` with `@places.where...` in your `index` action? – PinnyM Mar 11 '13 at 15:52
  • I guess I didn't read it carefully! Sorry! I thought the only change was .scoped, I didn't see the change of Place to @places, it works now! Thanks so much PinnyM and sorry about the confusion! – Farooq 6 hours ago – Farooq Mar 11 '13 at 22:22