Quite surprised to get the following error :
undefined method `[]' for #<CLLocation:0x9a95cd0> (NoMethodError)
when trying to reproduce the simplest example given :
BW::Location.get_once do |result|
p "From Lat #{result[:from].latitude}, Long #{result[:from].longitude}"
p "To Lat #{result[:to].latitude}, Long #{result[:to].longitude}"
end
CLLocation is imported (even if not sure it is required while using bubbblewrap) using the following :
app.frameworks += ["CoreLocation"]
as bubblewrap location is :
require 'bubble-wrap/location'
Any obviousness appreciated (:
BW::Location.get_once yields a CLLocation object, not an array of CLLocation.
Try something like this:
BW::Location.get_once do |location|
p "Lat #{location.latitude}, Long #{location.longitude}"
end
Related
I tried Google API using example bellow: https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&departure_time=1541202457&traffic_model=best_guess
It works fine but need to do the same example using Lat and Lon instead of real Address for Start and End.
What is the example of doing the same like above but using Lat and Lon?
Thanks.
Apparently I was testing at first with not correct Lat and Lon which was returning errors from google. This is working:
https://maps.googleapis.com/maps/api/directions/json?
origin=40.741963,-74.004915&
destination=40.812840,-74.074209&
departure_time=1541202457&
traffic_model=best_guess
So I have the following code:
new = #params[collection.to_s + '_attributes']
old = #model.send collection
if new.nil?
old.clear
else
new_records = new.map { |_, e| e[:id] }
if !new_records.nil? && !old.nil?
old.not_in(id: new_records).destroy_all
end
end
The problem is I didn't use 'push' function anywhere in my code and based on the stacktrace the error occurs when executing:
old.not_in(id: new_records).destroy_all
I'm new to Rails so I hope someone can help me. Thanks in advance!
UPDATE
I ended up using delete_all instead of destroy_all for now. I think it was causing the error. It's working now but it would really be nice if I could find out why it wasn't working with destroy_all.
I don't think not_in is rails command, Instead, try
old.where.not(id: new_records).destroy_all
or, not in can be used like this.
old.where('id NOT IN (?)',new_records).destroy_all
Try:
old.where.not(id: [new_records]).destroy_all
Not in always requires array.
I'm using the search function in geocoder, specifically the one for an IP address:
Geocoder.search("204.57.220.1")
When I type the command into the console and store the results as a variable I can view the contents:
[#<Geocoder::Result::Freegeoip:0x0000000241d848 #data={"ip"=>"204.57.220.1",
"country_code"=>"US", "country_name"=>"United States", "region_code"=>"WA",
"region_name"=>"Washington", "city"=>"Redmond", "zip_code"=>"98052",
"time_zone"=>"America/Los_Angeles", "latitude"=>47.674, "longitude"=>-122.122,
"metro_code"=>819}, #cache_hit=nil>]
but I can't figure out how to access the results. I've tried:
results.#data.zip_code, results[#data.zip_code], results[#data.results] and pretty much every other combonation I could think of. Any ideas?
Note that this returns an array. This means that you need to do something like:
results = Geocoder.search("204.57.220.1")
first_result = results.first
p first_result.data
p first_result.data['ip']
As Geocoder.search("204.57.220.1") is returning an array, you have to grab the first element of the array first, and then grab the data.
Do it this way:
results = Geocoder.search("204.57.220.1")
data = results.first.data
This will give you the data hash and from there you can extract your required data:
puts data['zip_code']
puts data['ip']
. . .
. . .
I am using the following step in my pagination method steps:
Edit : nodes2 is an array.
nodes = nodes2.take(per_page).offset((page_number.to_i - 1) * per_page)
#length = (nodes2.count/per_page).ceil
I get the following error:
undefined method 'offset' for #<Array:0x00000005905128>
Basically I am using the following steps and they work fine as I get the objects out of .leaves method, but am not sure how to deal with the array
nodes = inode.leaves.limit(per_page).offset((page_number.to_i - 1) * per_page)
#length = (inode.leaves.count/per_page).ceil
Could some one pls help me out. Thanks!
take is a method on Array. When you run it against nodes2 (which I assume is an ActiveRecord::Relation object), it's doing the equivalent of this:
nodes = nodes2.to_a.take(per_page)...
So because of this, offset is being run on an Array object. You could try making take(...) be the last method call, that way offset is still being run against ActiveRecord:
nodes = nodes2.offset((page_number.to_i - 1) * per_page).take(per_page)
I'm newvbie in ruby on rails.. I'm having problem with gsub.. I everytime I go to the list of my store page it says "undefined method `gsub' for nil:NilClass"..
here is mycode :
def self.search(search_val, page = 1)
#search_val = search_val.gsub("'", "\\\\'")
search_query = "store_id LIKE '%#{ #search_val }%' OR english_name LIKE '%#{ #search_val }%' OR chinese_name LIKE '%#{ #search_val }%'"
select("jos_store.id, store_id, english_name, chinese_name, store_manager, delivery_area,year, week").joins("LEFT OUTER JOIN (SELECT id as store_replenishment, store, MAX(stock_movement) AS stock_movement FROM jos_store_replenishment GROUP BY store) AS replenishment ON replenishment.store = jos_store.id").joins("LEFT OUTER JOIN jos_stock_movement ON jos_stock_movement.id = replenishment.stock_movement").where(search_query).order("year DESC, week DESC").paginate :page => page, :per_page => 15
end
thanks in advance
A good practice is doing .to_s when you are using string methods.
You can use the & operator on search_val. It allows you to avoid null pointer exceptions without adding additional checks or using to_s to convert a string to a string.
So, you'll have something like this:
#search_val = search_val&.gsub("'", "\\\\'")
You can read more on the safe navigation operator here: http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/
This means that search_val is in fact nil. You can easily verify this by printing out the value of search_val.
I'm not sure if this is your case, but the same undefined method gsub for nil:NilClass error happened with me after a few rollbacks and migrations.
Then, I restarted the server and works. Maybe this could be the case for some people that reached this topic searching on Google.