I’m trying to work out an issue and a bit stuck on the best way to
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
form has some address info that once saved we gather some Geo Location
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.
The issue I am having is that I have a before_save method in the Job
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
geo = APICall
errors.add(:base, “Geo Location Failed” ) if !geo.success
raise “Error” if !geo.success
Then in my controllers update action I have
@job = Job.find(params[:id])
flash[:notice] = "Successfully updated job."
return if has_session_return_to?
render :action => 'edit'
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
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
a variable so I know not to call it again when the user updates the form
again, or does someone have a better solution?
Maybe you can just make the API call only if the distance is blank? So,
the user manually sets the distance, the API call is not triggered.
Btw, you should prefer unless geo.success instead of if !geo.success.
Le mercredi 20 mars 2013 18:18:26 UTC+1, Ben Densmore a crit :
On Mar 20, 2013, at 11:18 AM, Ben Densmore wrote:
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
If it were me, I might keep two “global” variables (not session
variables, so stored in the db or in text files), for the timestamps of
the last successful transaction with the API, and the last failed
transaction with the API. Then you could look at those and decide
whether or not to try using the API.
You might also consider some AJAXy voodoo to use the API before
submitting the whole form…
(303) 722-0567 voice
So what I did for now was in my rescue statement I’ve added an instance
variable called @manual_edit_allowed. If that variable exists then I
allow the user to update the distance manually and the Geocode Location
is never called. I had to get something in quick because it was stopping
users from doing certain tasks. I hope to look more deeply into it now
it’s at least working.
Phillipe, I do try to use unless whenever possible. This is code I
inherited and yesterday was my first time working in that area, so I
haven’t started fixing things like that, especially since it works as
But I agree, it just looks cleaner using unless rather than if !
I appreciate the help on thinking this through.