ruby - How to avoid bug if geocoder sends me empty array when retrieving country of user (Rails 4/geocoder) -
I am preparing a daily deal rail app
I only display the user's deals I am thanking the country with the GeoCoder Gems.
I wonder what will happen if the Geocoder fails to recover the country and sends an empty array, as I think it fails to send the IP (see) < / P>
Class StaticPagesController & lt; ApplicationController def home @deals = deal_in_user_country.featured_on_hp response_to do format | Create an opportunity to filter out deals that meet the format.html # home.html.erb end and # user country def deal_in_user_country Deal.where (country: set_location_by_ip_lookup.country || 'United States') # If Geodor To pb if default = US version end and end
As you see, I tried to use And puts the 'United States', but I do not think it will work. I think if Geocoder sends blank array, set_location_by_ip_lookup = [] and then set_location_by_ip_lookup.country will generate an error, am I right?
How should I avoid the bug when I send an empty array of geo-word?
If it helps, for information, here's how I set the country in worries / CountrySureter
Extend the module CountrySester ActiveSupport :: Filter before_filter: set_location_by_ip_lookup end def Set_location_by_ip_lookup If Rails.env.development? Or Rails.env.test? Geocoder.search (request.remote_ip). First and #in production requests. Location End End End
Your code should be fine, if the geocoder is at least one Returns empty array (except I will not name this mehtod set_
because it is not set to anything)
Irb
{a : [: D]. First || : B} => {: A = & gt;: d} {a: [] First || : B} => {: A = & gt ;: b}
However, I will put it in Panathathy to make it clear
Deal. Where (country: (set_location_by_ip_lookup country || 'United States'))
Gecodoer.search
should not be thrown exception, otherwise it would be Geocoder.search ! / Code> According to an unwritten Ruby conference, I will double check it through the plugin from the Internet connection and see what happens
Comments
Post a Comment