TypeError:nil不是符号渲染:json

时间:2014-12-13 08:38:42

标签: ruby json ruby-on-rails-4

这是我的代码

 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: "raj@aol.in", 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: "tester@asdol.in", 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: "bobby@aol.com", 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}

在我的生产服务器中,但在我的localhost,开发服务器上运行良好。

这是log:

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'

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我解决了这个问题,这是因为表中没有主键,

在我在模型

中添加主键后,这个工作正常
class Supplier < ActiveRecord::Base
    self.primary_key = 'id'
   ......
   .... 
end