这种方法如何烘干呢?
def restore_download_delete_file
begin
case params[:submit]
when "restore"
restore_status = restore_file(params[:file_names])
raise if restore_status != 0
flash[:notice] = "File Successfully Restored."
redirect_to :action => "database_settings"
when "download"
download_status = download_file(params[:file_names])
raise if download_status != 0
when "delete"
delete_status = delete_file(params[:file_names])
raise if delete_status != 0
flash[:notice] = "File Successfully Deleted."
redirect_to :action => "database_settings"
end
rescue Exception => e
flash[:error] = "Error with #{params[:submit]}! Please retry."
redirect_to :action => "database_settings"
end
end
我该如何改进这个方法?
def restore_download_delete_file
begin
case params[:submit]
when "restore"
restore_status = restore_file(params[:file_names])
raise if restore_status != 0
flash[:notice] = "File Successfully Restored."
redirect_to :action => "database_settings"
when "download"
download_status = download_file(params[:file_names])
raise if download_status != 0
when "delete"
delete_status = delete_file(params[:file_names])
raise if delete_status != 0
flash[:notice] = "File Successfully Deleted."
redirect_to :action => "database_settings"
end
rescue Exception => e
flash[:error] = "Error with #{params[:submit]}! Please retry."
redirect_to :action => "database_settings"
end
end
How could I improve this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过将其分为四份来清理:一份用于恢复,一份用于删除,一份用于下载,一份用于调用适当的并处理异常。
另外,还有一些注意事项:
You can clean it up by dividing it into four: one for restore, one for delete, one for download, and one for calling the appropiate one and handling the exceptions.
Also, a couple considerations:
尝试这样
Try like this
你总是可以把你的大方法变成一个小解释器:
但我认为 egarcia 的方法最有意义;真的有
无需将所有这些东西混入一种方法中。共同点
确实非常小,所以一个控制器中有四种方法
更多意义:一种行动,一种方法。 DRY 只是一个指导方针,它不是
不惜一切代价必须遵循的教条。
You can always turn your mega-method into a little interpreter:
But I think egarcia's approach makes the most sense; there's really
no need to mash all this stuff into one method. The commonalities
really are quite minimal so four methods in one controller makes
more sense: one action, one method. DRY is just a guideline, it isn't
dogma to be followed at all costs.