Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

how do i display search results

Rails • Asked by Neil Patel

Following on from my prev question regarding implementing search using the geocoder gem [https://gorails.com/forum/how-do-i-implement-search-by-postcode-zipcode-using-the-geocoder-gem]

I have implemented a search feature on the index page. When i enter the postcode
it does not return any results . It gives me a url of http://localhost:3000/locations?utf8=%E2%9C%93&search=m20+2wz but nothing in the index view page.

locations_controller.rb

class LocationsController < ApplicationController
def index
if params[:search].present?
@locations = Location.near(params[:search], 10)
else
@locations = Location.all.order("created_at DESC")
end
end

** index.html.erb**

<div class="container">


SubContractor Postcode search


Use this postcode search to quickly find subcontractors in your area


<%= form_tag locations_path, :method => :get do %>
<%= text_field_tag :search, params[:search], placeholder: "e.g M20 2WZ" %>

    <%= submit_tag "Search Near", :name => nil %>
  </p>
<% end %>


location.rb

class Location < ApplicationRecord
geocoded_by :full_address
after_validation :geocode

def full_address
[address, city, postcode].compact.join(',')
end
end


added to index.html.erb now showing results

<% @locations.each do |location| %>


<%= location.company_name %>
<%= location.address %>
<%= location.city %>
<%= location.postcode %>

<% end %>

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.