2014-12-13 58 views
1

這裏是我的代碼類型錯誤:無不是一個符號渲染:JSON

def get_suppliers 
    supplier= Supplier.where(:user_id=>params[:supplier][:user_id],:is_deleted => "false") 
    if supplier.present?   
     render :json => {"status_code" => "200", :suppliers => supplier} 
     else 
     render :json => {"status_code" => "400", :suppliers => "Record not found"} 
     end  
    end 

//供應商價值

#<ActiveRecord::Relation [#<Supplier id: 69, supplier_name: "raj", food_business_no: "", mobile: "9154441222", work_phone: "9155551222 ", email_id: "[email protected]", date: "2014-11-18", other_information: "--", street_name: "VGP ", image_user_id: 1140, province: "", city: "", pincode: " ", state: "Australian capital teritory", country: "Australia", user_id: 42, supplier_user_id: 4, is_deleted: false, deleted_at: nil, created_at: "2014-11-18 09:26:56", updated_at: "2014-11-18 09:26:56">, #<Supplier id: 72, supplier_name: "tester", food_business_no: "", mobile: "3548621102", work_phone: "3549928500 ", email_id: "[email protected]", date: "2014-11-21", other_information: "--", street_name: "sdf ", image_user_id: 1166, province: "", city: "", pincode: " ", state: "Australian capital teritory", country: "Australia", user_id: 42, supplier_user_id: 5, is_deleted: false, deleted_at: nil, created_at: "2014-11-21 11:49:15", updated_at: "2014-11-21 11:49:15">, #<Supplier id: 74, supplier_name: "bobby", food_business_no: "", mobile: "9792626506", work_phone: "9852525252 ", email_id: "[email protected]", date: "2014-11-27", other_information: "--", street_name: "VIP ", image_user_id: 1175, province: "", city: "", pincode: " ", state: "Australian capital teritory", country: "Australia", user_id: 42, supplier_user_id: 7, is_deleted: false, deleted_at: nil, created_at: "2014-11-27 06:49:25", updated_at: "2014-11-27 06:49:25">] 

我在這一行越來越TypeError : nil is not a symbol

render :json => {"status_code" => "200", :suppliers => supplier} 

在我的生產服務器,但在我的本地主機,開發服務器中運行良好。

下面是日誌:

I, [2014-12-13T07:32:18.950505 #8580] INFO -- : Started POST "/get_suppliers" for 182.73.35.250 at 2014-12-13 07:32:18 +0000 
I, [2014-12-13T07:32:18.951363 #8580] INFO -- : Processing by SuppliersController#get_suppliers as */* 
I, [2014-12-13T07:32:18.951436 #8580] INFO -- : Parameters: {"access_token"=>"xxxxxx", "supplier"=>{"user_id"=>"42"}} 
I, [2014-12-13T07:32:18.954972 #8580] INFO -- : Completed 500 Internal Server Error in 3ms 
F, [2014-12-13T07:32:18.956617 #8580] FATAL -- : 
TypeError (nil is not a symbol): 
    app/controllers/suppliers_controller.rb:15:in `get_suppliers' 
+0

的胡亂猜測,但你可以嘗試'渲染:JSON => {:STATUS_CODE => 「200」:供應商=> supplier.to_json}' – shivam 2014-12-13 09:19:12

+0

@shivam不工作 – 2014-12-13 09:22:21

回答

0

我解決了這個問題,這造成的,因爲那裏是在沒有主鍵表,

這工作後,我在模型中添加主鍵

class Supplier < ActiveRecord::Base 
    self.primary_key = 'id' 
    ...... 
    .... 
end 
+2

如果你真的需要在模型本身上做到這一點?如果你使用Rails生成器創建模型,這不會是一個問題。 – 2014-12-23 14:48:21

+0

是@LeoCorrea我同意。 – 2014-12-24 05:34:45

1

supplier是一個局部變量的對象。請改變你的代碼 像

def get_suppliers 
@supplier= Supplier.where(:user_id=>params[:supplier][:user_id],:is_deleted => "false") 
if @supplier.present?   
    render :json => {"status_code" => "200", :suppliers => @supplier} 
    else 
    render :json => {"status_code" => "400", :suppliers => "Record not found"} 
    end  
end 

對不起我的壞

那麼可能是你需要改變status_code:status

+0

兩項建議不工作 – 2014-12-13 09:55:11

相關問題