Below is my class with which I use geocoder. I am having trouble checking if the address that is being transmitted through the geocoder is valid. This code works great when I add a company / edit a company that has addresses that, when transmitted through a geocoder, give latitude and longitude. But I need a code to work, so when a user tries to put an address that does not give latitude and longitude, neither when editing nor in creating a company, an error message appears and informs the user that the address is invalid.
Model
class Company < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 30 }
validates :website, presence: true
validates :address, presence: true
validates :description, presence: true
validates :primary_field, presence: true
geocoded_by :address
before_save :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
after_save :set_popup_value
private
...
end
View
<% provide(:title, 'Add Company') %>
<h1>Add Company</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@company) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :website %>
<%= f.text_field :website %>
<%= f.label :primary_field %>
<%= f.select :primary_field, @primary_field %>
<%= f.label :address %>
<%= f.text_field :address, :placeholder => '123 Test St, City State Zip'%>
<%= f.label :description %>
<%= f.text_area :description, :size => "30x5" %>
<%= f.submit "Add Company", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
source
share