【问题标题】:Rails equivalent of respond_to |format| with a straight renderRails 等效于 respond_to |format|直接渲染
【发布时间】:2010-04-29 17:57:16
【问题描述】:

我经常使用 ActiveResource,所以我的服务模型只使用 XML。因此,我不需要 respond_to 块,我只是 render :xml => @model

但是我不知道如何使用它来渲染 404 标头。我不得不求助于 respond_to,我认为这增加了一些不必要的方法调用。这是我正在使用的:

respond_to do |format|
  if (record_found)
    render :xml => @some_record
  else
    format.xml{ head :not_found }
  end
end

但我只想要 render :xml => head :not_found 这样的东西,但它不起作用。谁能告诉我正确的语法?

【问题讨论】:

    标签: ruby-on-rails xml rendering http-status-code-404 actioncontroller


    【解决方案1】:

    你试过了吗:

    render {:xml => "Record not found", :status => :not_found }
    

    【讨论】:

    • 你从我的问题中得到了很多分数!!谢谢,我没有意识到直弦会起作用
    猜你喜欢
    • 2015-08-16
    • 2015-11-24
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2015-01-04
    • 2011-06-25
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多