【问题标题】:AbstractController::DoubleRenderError is being reported even when using redirect_to and returnAbstractController::DoubleRenderError 即使在使用 redirect_to 并返回时也会被报告
【发布时间】:2015-01-31 06:08:17
【问题描述】:

我收到了这个错误

AbstractController::DoubleRenderError: Render and/or redirect were called 
multiple times in this action. Please note that you may only call render 
OR redirect, and at most once per action. Also note that neither redirect 
nor render terminate execution of the action, so if you want to exit an 
action after redirecting, you need to do something like "redirect_to(...) 
and return"

您可以在下面找到控制器、模型和视图

这是模型(app/models/upload_file.rb)

class UploadFile

  def self.save(uploadData)
    @success = true;

    begin
      name = uploadData.original_filename
      directory = 'public/data'
      # create the file on the server
      path = File.join(directory, name)
      # write the file
      File.open(path, "wb") { |f| f.write(uploadData.read) }

    rescue
      @success = false;
    end

    return @success;
  end

  def cleanup
    #Borrar el archivo una vez se haya procesado (ojo con los que se analizan pero tienen errores, tambien se deben borrar)
  end
end

这是控制器的相关部分(app/controllers/employees_controller.rb)

  def upload
    add_breadcrumb 'Cargar Archivo', :employees_upload_path
    render 'employees/uploadFile'
  end

  def uploadFile
    @exito = UploadFile.save(params['upload']['datafile'])

    if (@exito)
      redirect_to employees_uploadValidate_path and return
    end
  end

这是视图 (app/views/employees/uploadFile.html.erb)

<script type="text/javascript">
    function validateFileName() {
        var fileName ;
        var valido ;

        fileName = document.getElementById("upload_datafile").value ;

        //Revisar que el archivo tenga la extension esperada
        valido = (fileName !== undefined) && (fileName.length >= 5) && (endsWith(fileName.toUpperCase(),".XLS") || endsWith(fileName.toUpperCase(),".XLSX")) ;

        if (!valido) {
            alert('Debe elegir un archivo de EXCEL (XLS o XLSX)')
        }

        return valido ;
    }

    function endsWith(str, suffix) {
        return str.indexOf(suffix, str.length - suffix.length) !== -1;
    }
</script>
<div class="left p-breadcrumbs">
  <%= render_breadcrumbs :separator => ' > ' %>
</div>
<div class="row margin-row">
  <div class="separador"></div>
  <div class="row small-12 centered">
    <div class="small-8 columns">
      <h1>Cargar Archivo</h1>
    </div>

  </div>
  <div class="row small-12 centered">
    <%= form_tag({:action => 'uploadFile'}, :multipart => true, :onsubmit => 'return validateFileName();') do %>
        <p><label for="upload_file">Seleccione el archivo a cargar:</label>
          <%= file_field 'upload', 'datafile' %></p>
        <%= submit_tag 'Subir Archivo' %>
    <% end %>
  </div>
</div>

在我的 routes.rb 中我有以下几行

  get 'employees/upload'
  post 'employees/uploadFile'
  get 'employees/uploadValidate'

我不明白为什么如果我有一个 redirect_to 和 return 会导致 DoubleRenderError

事实上,错误正是在该行报告的

redirect_to employees_uploadValidate_path and return 

【问题讨论】:

  • 哪一行导致错误?
  • @YanFoto 在redirect_to employees_uploadValidate_path 报错并返回

标签: ruby-on-rails ruby file-upload


【解决方案1】:

事实证明,不遵循 Ruby/Rails 约定使用underscore_case 作为方法视图的名称控制器导致了问题。

为了解决这个问题,我将 uploadFile 方法重构为 upload_file 并重命名视图以匹配此约定。我将同样的方法应用于upload_validate 方法和视图,这解决了问题。

如果您想了解有关此主题的更多信息,我会发布此链接

【讨论】:

    【解决方案2】:

    我将在这里大胆猜测。我相信错误发生在您upload 方法上。如果我们仔细看看那里,似乎add_breadcrumb 方法正在使用路径将您重定向到某个地方,之后,它仍然调用render 方法。

    【讨论】:

    • Finks,面包屑不会重定向,它会创建一个指向该页面的链接以允许在不同选项之间导航
    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2012-01-26
    • 2015-11-27
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多