Ask A Question


You’re not receiving notifications from this thread.

how do i display search results

Neil Patel asked in Rails

Following on from my prev question regarding implementing search 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.


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

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


class Location < ApplicationRecord
geocoded_by :full_address
after_validation :geocode

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


added to index.html.erb now showing results

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

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

<% end %>

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.