加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:20:07 所属栏目:百科 来源:网络整理
导读:在控制器中: 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-
在控制器中:

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 context和 render method in the controller context是两种不同的方法,前者不期望:json选项.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读