我面临着非常奇怪的问题。
我有两个 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)
它完美地返回了出发点。
如果我使用 getJSON 执行 ajax 请求(您可以看到数据被询问为 JSON 并且查询正在运行:
在 2013-09-28 09:59:50 +0200 开始 GET "/departures?query=l" for 127.0.0.1 +0200 由 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)). If you want to get an array of records from a relation, you can call #to_a (e.g.
.load Post.where(published: true).to_a). (called from c:in
find' 调用) 出发负载 (3.0ms) SELECTdepartures
.* FROMdepartures
WHERE (name LIKE '%l%') # # # # # # # 渲染的离开/index.json.jbuilder (3.0ms) 在 54ms 内完成 200 OK (Views: 38.0ms | ActiveRecord: 3.0ms)
它也有效。
但是,如果我添加 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)). If you want to get an array of records from a relation, you can call #to_a (e.g.
.load Post.where(published: true).to_a). (called from c:in
find':) 调用 Departure Load (3.0ms) SELECTdepartures
.* FROMdepartures
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
现在,它起作用了。问题是当我尝试对数据库视图做同样的事情时。但是,我需要这个视图,但我不知道如何解决它。