[英]Render partial Error in ruby on rails
我有以下方法,如果請求是針對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
這是局部方法
def partial
'messages/show'
end
如果我使用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]}
當我請求test.xml
但我想使用程式化的html錯誤。 我在這里做錯了什么?
我認為您應該執行以下操作:
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.