作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 gem 'roo' 上传文件。当我检查类型的文件时,我在实例方法中有这个:
def open_spreadsheet
case File.extname(file.original_filename)
when ".xls" then Roo::Excel.new(file.path, file_warning: :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, file_warning: :ignore)
when ".csv" then Roo::CSV.new(file.path, file_warning: :ignore)
when ".ods" then Roo::LibreOffice.new(file.path,file_warning: :ignore)
else raise "Unknown file type"
end
end
有没有办法捕获此异常,以便用户只看到消息并重试,而不会实际引发语法错误?
最佳答案
你可以有catch block
begin
# call methods
rescue ErrorType => error_object
# handle the error
end
您还可以在 begin rescue
block 中定义事务
begin
ActiveRecord::Base.transaction do
# call methods
end
rescue ErrorType => error_object
# handle the error
end
只有当没有错误发生时,方法才会持久化到数据库
同时检查 Rails errors
关于ruby-on-rails - 如何在 Rails 应用程序中引发错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675602/
我是一名优秀的程序员,十分优秀!