在rails 5.1及更高版本中使用什么而不是`render:text`(以及`render nothing:true`)?

时间:2017-04-15 17:18:17

标签: ruby-on-rails ruby ruby-on-rails-5

Rails 5.1 removes a whole load of previously deprecated methods。其中有老朋友render :text

当您需要呈现某些文本时,它非常有用,但不需要视图模板的开销。例子:

render text: "ok"
render text: t('business_rules.project_access_denied'), status: 401

反而使用什么?

1 个答案:

答案 0 :(得分:94)

不推荐使用的方法是使用render :plain

Rails Guide on Layouts and Rendering

  

2.2.6渲染文本

     

您可以使用:plain选项将纯文本(完全没有标记)发送回浏览器:

render plain: "OK"

加成

而不是render nothing: true(也已删除),现在应该使用head :ok。同样的事情:发送http 200响应代码,只发送标题,没有内容。