3
奇怪的問題。如果底層的類是一個模塊,將Json拆分成沒有問題,如果它只是方法,也可以,但問題在於,當它是一個類時,它不再拆分Json,並返回一個空數組..但是,如果作爲一個班級,我做了一個放置對象,它實際上把它.. 任何想法爲什麼?我該如何解決它?Rails API不會拆分Json
我有這樣的控制器:
def index
begin
call_employee_work_locations_api
rescue => ex
render :json => {"service unavailable": "0001" }, :status => :service_unavailable
end
end
我有這樣的服務:
def call_employee_work_locations_api
auth = {:username=>ENV["USERNAME"], :password=>ENV["PASSWORD"]}
employee_locations = HTTParty.get(employee_work_Location_url , :basic_auth => auth)
#serialize_work_location(employee_locations)
serializer = EmployeeSerializer.new
serializer.serialize_work_location(employee_locations)
end
我有這樣的建設者:
json.array!(@top_locations) do |location|
json.extract! location, :name, :description, :latitude, :longitude
end
我有這個類:
class EmployeeSerializer
def serialize_work_location(employee_locations)
employee_locations= JSON.parse(employee_locations)
locations=[]
employee_locations["work_locations"].each do |attributes|
location = Location.new(attributes["latitude"],attributes["longitude"],attributes["description"],attributes["name"])
locations.push(location)
end
employee_locations_selector(locations)
end
def top_office_location_selector(locations, city)
top_locations=[]
locations.each do |office|
if office.name == city[0] then top_locations.push(office) end
if office.name == city[1] then top_locations.push(office) end
end
@top_locations = top_locations
p @top_locations <--- it prints the object perfectly, but does not pass to the view, I get an empty array instead.
end
def employee_locations_selector(locations)
city = locations.each_with_object(Hash.new(0)) { |locations, counts| counts[locations.name] += 1 }.max_by{|k,v| v}
top_office_location_selector(locations, city)
end
end
我不敢相信我沒有看到..老實說:| |非常感謝 – Hedu