Search code examples
ruby-on-railsxmlrenderinghttp-status-code-404actioncontroller

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. Thus, I have no need for a respond_to block, I literally just render :xml => @model

I can't however figure out how to render a 404 header using this. I've had to resort to respond_to, which I think adds a few unnecessary method calls. 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. Can anyone tell me the proper syntax?


Solution

  • Have you tried:

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