我收到此错误
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并且返回导致DoubleRenderError
事实上,正好在该行报告错误
redirect_to employees_uploadValidate_path and return
答案 0 :(得分:1)
事实证明不遵循Ruby / Rails约定使用underscore_case
作为方法和视图的名称控制器导致问题。
要解决此问题,我将uploadFile
方法重构为upload_file
并重命名视图以匹配此约定。我将相同的方法应用于upload_validate
方法和视图,这解决了问题。
如果您想了解有关此主题的更多信息,请发布此链接
答案 1 :(得分:0)
我会在这里猜测一下。我相信错误发生在您upload
方法上。如果我们仔细查看那里,似乎add_breadcrumb
方法正在使用路径重定向到某个地方,之后,它仍然会调用render
方法。