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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GP1gLRhHtZ4J. For more options, visit https://groups.google.com/groups/opt_out.
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 solution?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... -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Philippe Guégan
2013-Mar-21 07:33 UTC
Re: What would be the best way to handle this situation?
Hi, Maybe you can just make the API call only if the distance is blank? So, if the user manually sets the distance, the API call is not triggered. Btw, you should prefer unless geo.success instead of if !geo.success. Phil. Le mercredi 20 mars 2013 18:18:26 UTC+1, Ben Densmore a écrit :> > 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/LlXf9p6s9L0J. For more options, visit https://groups.google.com/groups/opt_out.
Ben Densmore
2013-Mar-21 15:47 UTC
Re: What would be the best way to handle this situation?
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 just allow the user to update the distance manually and the Geocode Location API 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 that 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 is. But I agree, it just looks cleaner using unless rather than if ! I appreciate the help on thinking this through. Ben On Wednesday, March 20, 2013 1:18:26 PM UTC-4, Ben Densmore wrote:> > 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/MptXLDuMEugJ. For more options, visit https://groups.google.com/groups/opt_out.