ruby-on-railsrubygeokit

Using Geokit and Geocoding to print value


I have a Model Stop as such.

 class Stop < ActiveRecord::Base
    geocoded_by :address
    after_validation :geocode
    acts_as_mappable defaults_units: :kms, lat_column_name: :latitude, lng_column_name: :longitude

My stop table has field id,name,address,longitude,latitude

This table contains pre-populated data of some bus stops. I have a small form where the user enters an address.

<%= form_tag("/welcome/index", method: "post") do %>
  <h5>Current Location</h5>
  <%= text_field_tag :address %>
  
  <p>
    <%= submit_tag("Submit") %>
  </p>


<% end %>

Then in my controller i capture this address entered by user and use GEOKIT to find the bus stop that is closest to the user provided address as such.

curAddress = params[:address] 
@curlonglat = Geocoder.coordinates(curAddress)
@answer = Stop.closest(origin: @curlonglat)

Finally i try to print the stop that is the closest in my view file as such

<% if @answer%>
<%= "#{@answer} "%>
<% end %>

My output is #<Stop::ActiveRecord_Relation:0x007faa485228e0>

I am not sure what i did wrong?


Solution

  • As @answer is a instance of ActiveRecord_Relation:, so it looks you have to do it like this

    <%= @answer.first.address %>