2012-01-11 64 views
0

我正在嘗試編寫利用從控制器呈現的JSON的api代碼。我能夠在本地成功獲得我想要的結果,但是當我推送到Heroku時,我的JSON部分無法正確顯示。API代碼無法正確地從控制器渲染JSON

爲了加入上下文,我試圖創建一個與膳食信息(名稱,ID等)和照片網址嵌套的JSON。本地照片網址在JSON中正確呈現。但是,在Heroku上,照片網址顯示爲空。在Heroku上,我也測試過僅渲染url JSON,它正確地獲取了url。

如果你知道它爲什麼在本地顯示正確而不是在Heroku上,請告訴我。謝謝

創建我的JSON方式如下:

def api 
@meals = Meal.all 
@urls = Hash.new 
@return_val = Array.new 

@sorted_meals = Meal.select('meals.name as meal_name, meals.id as meal_id, 
          COALESCE(sum(meal_ratings.rating), 0) as meal_rating, 
          restaurants.id as restaurant_id, restaurants.name as restaurant_name'). 
        joins('LEFT JOIN meal_ratings ON meals.id = meal_ratings.meal_id 
          LEFT JOIN restaurants ON restaurants.id = meals.restaurant_id'). 
        group('meals.name, meals.id, restaurants.id, restaurants.name'). 
        order('meal_rating DESC').all 

@meals.each do |meal| 
    unless meal.meal_photos.empty? 
    @urls[meal.id] = {"thumb" => meal.meal_photos[0].photo.url(:thumb), "profile" => meal.meal_photos[0].photo.url(:profile)} 
    end 
end 

@sorted_meals.each do |meal| 
    @return_val.push("id" => meal.meal_id, "name" => meal.meal_name, 
        "rating" => meal.meal_rating, "restaurant" => meal.restaurant_name, 
        "restaurant_id" => meal.restaurant_id, "urls" => @urls[meal.meal_id]) 
end 

respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @return_val } # render json: @url render json: @meals 
end 

回答

0

的問題是由於Postgres的。當我查詢一次meal_id時,它返回一個字符串,並且沒有用作散列鍵,所以我得到了零。將id字符串變成int後,一切正常。感謝Sena的幫助,這已經得到解決。