即使使用 redirect_to 和 return 也会报告 AbstractController::DoubleRenderError
AbstractController::DoubleRenderError is being reported even when using redirect_to and return
我遇到了这个错误
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
我要大胆猜测一下。我相信错误发生在你 upload
方法上。如果我们仔细观察那里,似乎 add_breadcrumb
方法正在使用路径将您重定向到某个地方,之后,它仍然调用 render
方法。
事实证明,不遵循使用 underscore_case
作为 方法名称的 Ruby/Rails 约定 和控制器的 views 导致了问题。
为了解决这个问题,我将 uploadFile
方法重构为 upload_file
并重命名视图以匹配此约定。我将相同的方法应用于 upload_validate
方法和视图,这解决了问题。
我 post 这个链接,以防您想了解更多关于这个主题的信息
我遇到了这个错误
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
我要大胆猜测一下。我相信错误发生在你 upload
方法上。如果我们仔细观察那里,似乎 add_breadcrumb
方法正在使用路径将您重定向到某个地方,之后,它仍然调用 render
方法。
事实证明,不遵循使用 underscore_case
作为 方法名称的 Ruby/Rails 约定 和控制器的 views 导致了问题。
为了解决这个问题,我将 uploadFile
方法重构为 upload_file
并重命名视图以匹配此约定。我将相同的方法应用于 upload_validate
方法和视图,这解决了问题。
我 post 这个链接,以防您想了解更多关于这个主题的信息