为什么渲染:view.json.erb中的json导致错误,想要:partial,:template,:inline

时间:2013-01-07 12:19:02

标签: ruby-on-rails ruby ruby-on-rails-3

在控制器中:

def some_action
  @foo = 'bar'
end

some_action.json.erb

<%= render :json => @foo %>

这会导致错误:

ArgumentError - You invoked render but did not give any of :partial, :template, :inline, :file or :text option.:
  (gem) actionpack-3.2.10/lib/action_view/renderer/template_renderer.rb:36:in `ActionView::TemplateRenderer#determine_template'
  (gem) actionpack-3.2.10/lib/action_view/renderer/template_renderer.rb:10:in `ActionView::TemplateRenderer#render'
  (gem) actionpack-3.2.10/lib/action_view/renderer/renderer.rb:36:in `ActionView::Renderer#render_template'
  (gem) actionpack-3.2.10/lib/action_view/renderer/renderer.rb:17:in `ActionView::Renderer#render'
  (gem) actionpack-3.2.10/lib/action_view/helpers/rendering_helper.rb:24:in `#<Class:0xb4a2378>#render'
   ...  

尽管我绕过它,但我不明白为什么render :json => ...不应该在这里工作。我知道我可以直接从控制器为format.json渲染,我试图通过一个视图来做,而不是手动制作.erb中的json。

Ruby 1.9上的Rails 3.2.10

1 个答案:

答案 0 :(得分:3)

render method in a view contextrender method in the controller context是两种不同的方法,前者不期望:json选项。

建议使用apneadiving,使用@foo.to_json或json构建器。

相关问题