简体   繁体   English

Rails等效于response_to | format | 直线渲染

[英]Rails equivalent of respond_to |format| with a straight render

I'm working with ActiveResource a lot so my service models are only using XML. 我经常使用ActiveResource,因此我的服务模型仅使用XML。 Thus, I have no need for a respond_to block, I literally just render :xml => @model 因此,我不需要render :xml => @model块,我实际上只是render :xml => @model

I can't however figure out how to render a 404 header using this. 但是,我无法弄清楚如何使用此来呈现404标头。 I've had to resort to respond_to, which I think adds a few unnecessary method calls. 我不得不诉诸response_to,我认为这增加了一些不必要的方法调用。 Here's what I'm using: 这是我正在使用的:

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

but I just want something like render :xml => head :not_found which doesn't work. 但是我只想要像render :xml => head :not_found这样的东西,它不起作用。 Can anyone tell me the proper syntax? 谁能告诉我正确的语法?

你有没有尝试过:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM