微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在控制器中:

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'
   ...

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

Ruby 1.9上的Rails 3.2.10

解决方法

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

apneadiving所示,使用@ foo.to_json或json构建器.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐