简体   繁体   English

呈现红宝石的部分错误

[英]Render partial Error in ruby on rails

I have the following method, to throw a 404 if the request is made for xml,json or html. 我有以下方法,如果请求是针对xml,json或html的,则抛出404。

def render_error(code, status_type = nil)
  @error = ErrorMessage.new(code, status_type)
  respond_to do |format|
    format.any(:html, :json) { render @error.partial, status: @error.status }
    format.any { render html: ErrorMessage.new(404).partial }
  end
end

This is the partial method 这是局部方法

def partial
  'messages/show'
end

The same thing works if I use format.any { head 404, "content_type" => 'text/plain' } instead of format.any { render html: ErrorMessage.new(404).partial } .This is the error I get. 如果我使用format.any { head 404, "content_type" => 'text/plain' }而不是format.any { render html: ErrorMessage.new(404).partial }同样的format.any { render html: ErrorMessage.new(404).partial } 。这是我得到的错误。

Missing template errors/index with {:locale=>[:en], :formats=>[:xml], :handlers=>[:erb, :builder, :raw, :ruby, :haml]}

when I request for test.xml But I want to use stylized html error. 当我请求test.xml但我想使用程式化的html错误。 What am I doing wrong here? 我在这里做错了什么?

我认为您应该执行以下操作:

    format.any { render :html => 'messages/show', object: ErrorMessage.new(404,nil) }

明白了。这是format.any { render file: @error.partial, content_type: 'text/html', formats: [:html], status: @error.status }

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

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