Rails控制器渲染json错误:nil不是符号

时间:2013-09-27 21:59:21

标签: ruby-on-rails json

我面临着非常奇怪的问题。

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

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

BoatController:

def index

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

然而,当我对离线做同样的事情,比如服务器:3000 /离开,

 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获得的。

我得到的不是一个符号:

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 =&gt; @departures。

我测试了下一个组合: 1.使用渲染:json =&gt; @departures评论道。在我的浏览器中:服务器:3000 /离开

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”=&gt;“l”}   弃用警告:不推荐调用#find(:all)。请直接致电#all。您还使用了finder选项。这些也被弃用了。请建立一个   范围而不是使用finder选项。 (来自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(已发布:true).load ). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(已发布:true).to_a ). (called from c:in find':)   出发负载(3.0ms)SELECT departures。* FROM departures WHERE(名称LIKE'%l%')   #   #   #   #   #   #   #   渲染的离开/ index.json.jbuilder(3.0ms)   在54ms完成200 OK(浏览次数:38.0ms | ActiveRecord:3.0ms)

  2. 它也有效。

    1. 但是,如果我添加渲染:json =&gt; @departures然后在两种情况下(从浏览器或javascript调用)我得到相同的错误结果:

      在2013-09-28 10:02:52 +0200开始获取127.0.0.1的“/离开”   由DeparturesController处理#index为HTML   弃用警告:不推荐调用#find(:all)。请直接致电#all。您还使用了finder选项。这些也被弃用了。请建立一个   范围而不是使用finder选项。 (来自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(已发布:true).load ). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(已发布:true).to_a ). (called from c:in find':)   出发负载(3.0ms)SELECT departures。* FROM departures WHERE(名称LIKE'%%')   #   #   #   #   #   #   #   #   #   在69毫秒内完成500内部服务器错误

      TypeError(nil不是符号):   app / controllers / departures_controller.rb:12:在`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)   在救援/布局(153.0ms)内呈现c:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack- 4.0.0 / lib / action_dispatch / middleware / templates / rescues / diagnostics.erb

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

      2º更新

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

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

      现在,它有效。问题是当我尝试对数据库视图执行相同操作时。但是,我需要这个观点,我不知道如何解决它。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我已经修好了!

正如我告诉过你的那样,离开模型与数据库视图相关联。实际上,我并不完全确定我的工作是什么。

我做了什么:

  1. 我选择了离港视图。
  2. 我再次支持Departures模型。
  3. 我db:迁移它。 (我看到它创建了一个created_at和updated_at,这可能是问题所在。)
  4. 我放弃了离境表。
  5. 我使用与表相同的结构创建了Departures视图(这次添加带有虚拟值的created_at和updated_at列,因为我不在乎)。
  6. 这就是全部,当我做渲染时:json =&gt; @departures现在有效!!