Thursday, March 21, 2013

[Rails] 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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate