【发布时间】: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