【问题标题】:Render partial Error in ruby on rails在 ruby​​ on rails 中渲染部分错误
【发布时间】:2015-03-17 19:22:49
【问题描述】:

我有以下方法,如果请求是针对 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 },同样的效果。这是我得到的错误。

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

当我请求test.xml 但我想使用程式化的 html 错误。我在这里做错了什么?

【问题讨论】:

  • 我们不知道。你的错误是什么?
  • 对此我很抱歉。我忘记在此处粘贴错误。现在已经更新了问题。谢谢

标签: ruby-on-rails ruby renderpartial


【解决方案1】:

知道了..这行得通 format.any { render file: @error.partial, content_type: 'text/html', formats: [:html], status: @error.status }

【讨论】:

    【解决方案2】:

    我认为你应该这样做:

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

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多