【问题标题】:Rails controller render json Error: nil is not a symbolRails 控制器渲染 json 错误:nil 不是符号
【发布时间】:2013-09-27 21:59:20
【问题描述】:

我遇到了一个很奇怪的问题。

我有两个 Rails 控制器,当我尝试将一组从 db 恢复的对象渲染为 Json 时,一个正在工作,另一个使用相同的代码。

当我在浏览器中编写 server:3000/boats 时的工作示例,我得到所有 json 格式的数据库船。

船控制器:

def index

  @boats = Boat.all
  puts @boats
  render :json => @boats          
end

但是,当我对出发地做同样的事情时,比如 server:3000/departures,

 def index
 if params[:query]
   @departures = Departure.find(:all, :conditions => ['name LIKE ?', "%#   {params[:query]}%"])
  else
  @departures = Departure.all
 end

 puts @departures
 render :json => @departures    
end

唯一的区别是 DB 中的船是一张桌子,而离开是一张视图。但我认为这不是问题,因为在跟踪中您可以看到偏离是从 DB 获得的。

我得到 nil 不是一个符号:

Started GET "/departures" for 127.0.0.1 at 2013-09-27 23:54:02 +0200
Processing by DeparturesController#index as HTML
Departure Load (2.0ms)  SELECT `departures`.* FROM `departures`
#<Departure:0x0000000407a068>
#<Departure:0x00000004079168>
#<Departure:0x00000004083ca8>
#<Departure:0x00000004081a98>
#<Departure:0x0000000408b250>
#<Departure:0x00000004090458>
#<Departure:0x000000040a0b28>
#<Departure:0x000000040b2d78>
#<Departure:0x000000040b05c8>
Completed 500 Internal Server Error in 10ms

TypeError (nil is not a symbol):
app/controllers/departures_controller.rb:14:in `index'

有什么建议吗?

更新: 第 14 行是:render :json => @departures.

我已经测试了以下组合: 1. 带有render :json => @departures 评论。在我的浏览器中:server:3000/departures

Started GET "/departures" for 127.0.0.1 at 2013-09-28 09:48:08 +0200
Processing by DeparturesController#index as HTML
DEPRECATION WARNING: Calling #find(:all) is deprecated. Please call #all directly  instead. You have also used finder options. These are also deprecated. Please build a
scope instead of using finder options. (called from c:in `find':)
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation,    you can call #load (e.g. `Post.where(published: true).load`). If you want to get
an array of records from a relation, you can call #to_a (e.g. `Post.where(published:  true).to_a`). (called from c:in `find':)
Departure Load (3.0ms)  SELECT `departures`.* FROM `departures` WHERE (name LIKE '%%') 
#<Departure:0x000000041017c0>
#<Departure:0x00000004100410>
#<Departure:0x00000004109fb0>
#<Departure:0x00000004110ab8>
#<Departure:0x0000000411a680>
#<Departure:0x000000041291f8>
#<Departure:0x00000004132988>
#<Departure:0x00000004131bc8>
#<Departure:0x00000004131150>
Rendered departures/index.html.erb within layouts/application (7.0ms)
Completed 200 OK in 66ms (Views: 53.0ms | ActiveRecord: 3.0ms)

完美返回出发点。

  1. 如果我使用 getJSON 进行 ajax 请求(您可以看到数据被询问为 JSON 并且查询正在运行:

    在 2013-09-28 09:59:50 +0200 开始 GET "/departures?query=l" for 127.0.0.1 由 DeparturesController#index 处理为 JSON 参数:{"query"=>"l"} 弃用警告:不推荐调用#find(:all)。请改为直接调用#all。您还使用了查找器选项。这些也已弃用。请建一个 范围而不是使用查找器选项。 (来自 c:in find':) DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g.Post.where(published: true).load). If you want to get an array of records from a relation, you can call #to_a (e.g.Post.where(published: true).to_a). (called from c:infind':) 出发负载 (3.0ms) SELECT departures.* FROM departures WHERE (name LIKE '%l%') # # # # # # # 渲染离开/index.json.jbuilder (3.0ms) 在 54 毫秒内完成 200 次 OK(查看次数:38.0 毫秒 | ActiveRecord:3.0 毫秒)

它也有效。

  1. 但是,如果我添加 render :json => @departures 那么在这两种情况下(从浏览器或 javascript 调用)我都会得到相同的错误结果:

    在 2013-09-28 10:02:52 +0200 开始 GET "/departures" for 127.0.0.1 DeparturesController#index 处理为 HTML 弃用警告:不推荐调用#find(:all)。请改为直接调用#all。您还使用了查找器选项。这些也已弃用。请建一个 范围而不是使用查找器选项。 (来自 c:in find':) DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g.Post.where(published: true).load). If you want to get an array of records from a relation, you can call #to_a (e.g.Post.where(published: true).to_a). (called from c:infind':) 出发负载 (3.0ms) SELECT departures.* FROM departures WHERE (name LIKE '%%') # # # # # # # # # 在 69 毫秒内完成 500 个内部服务器错误

    TypeError(nil 不是符号): app/controllers/departures_controller.rb:12:in `index'

    渲染 c:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (64.0ms) 渲染 c:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (4.0ms) 渲染 c:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms) 在救援/布局中渲染 c:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack- 4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (153.0ms)

如果我使用另一个模型进行查询,则该方法有效。正如我所说,我认为这与离开是一个数据库视图而不是其他模型的表这一事实有关。

2º更新

我已经用一个表替换了数据库视图,这是控制器:

def index
@departures = Departure.all
render :json => @departures
end

现在,它可以工作了。问题是当我尝试对数据库视图做同样的事情时。但是,我需要这个视图,我不知道如何解决它。

【问题讨论】:

  • 或许也可以在这里发布您的出发模型?
  • 您的离开控制器#index 代码中的哪一行是第 14 行?该行是错误所在。
  • 尝试呈现为 json 的那个。渲染 :json => @departures
  • 我的出发模型是空的:class Departure

标签: ruby-on-rails json


【解决方案1】:
def index
  query = params[:query] || ""
  @departures = Departure.find(:all, :conditions => ['name LIKE ?', "%#{query}%"])
end

【讨论】:

  • 感谢您的回复 Elga,但这不是重点。控制器正在从数据库中正确获取值。问题是在这一行 [@render :json => @departures] 我尝试将值呈现给 Json。这很奇怪,但我对其他一些模型做了同样的测试,它们都可以工作。所有这些模型之间的唯一区别是它们都链接到 DB 表,除了链接到 View 表的离开。
【解决方案2】:

我已经解决了!

正如我告诉你的那样,这与离开模型链接到数据库视图这一事实有关。实际上,我并不完全确定我是怎么做到的。

我做了什么:

  1. 我放下 Departures 视图。
  2. 我再次搭建 Departures 模型。
  3. 我 db:迁移它。 (我看到它创建了一个 created_at 和 updated_at 也许这就是问题所在)。
  4. 我删除了 Departures 表。
  5. 我创建了与表结构相同的 Departures 视图(这次添加了带虚拟值的 created_at 和 updated_at 列,因为我不在乎)。

仅此而已,当我渲染时 :json => @departures 现在可以工作了!!

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 2011-05-14
    • 2013-01-27
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多