Hi All,
I’m trying to work out an issue and a bit stuck on the best way to
handle
this, so I’m hoping someone can give me some ideas to try.
I have a form that contains some info about a particular job. Part of
this
form has some address info that once saved we gather some Geo Location
info
so we can plot the distance.
We had an issue where the Geo API was down and therefore we couldn’t get
the distance in miles from the base office to a customers location for
plotting on a map.
I was asked to raise an error and bring the user back to the form to
manually add in the distance if the API is not responding correctly.
Pretty
straight forward.
The issue I am having is that I have a before_save method in the Job
Model
that runs this geo location code. If the API is having issues I Raise an
error and handle it in the controller
So the Model has
before_save :geolocation_address
def geolocation_address
geo = APICall
errors.add(:base, “Geo Location Failed” ) if !geo.success
raise “Error” if !geo.success
end
Then in my controllers update action I have
@job = Job.find(params[:id])
if @job.update_attributes(params[:job])
flash[:notice] = "Successfully updated job."
return if has_session_return_to?
redirect_to @job.contact
else
render :action => 'edit'
end
rescue
render :action => 'edit'
This brings the user back to the form to manually edit which is what I
want. But the problem is that I’m always going to go back into the
rescue
block as long as the Geo location fails.
Should I pull that before_save out and manually call the
geolocation_address in the controller then when I hit the rescue block
pass
a variable so I know not to call it again when the user updates the form
again, or does someone have a better solution?
Thanks,
Ben