Ruby on rails 我怎样才能用这种方法烘干?
如何改进此方法?您可以将其分为四个部分来进行清理:一个用于恢复,一个用于删除,一个用于下载,另一个用于调用适当的方法并处理异常Ruby on rails 我怎样才能用这种方法烘干?,ruby-on-rails,ruby,dry,Ruby On Rails,Ruby,Dry,如何改进此方法?您可以将其分为四个部分来进行清理:一个用于恢复,一个用于删除,一个用于下载,另一个用于调用适当的方法并处理异常 def restore_download_delete_file begin case params[:submit] when "restore" restore_status = restore_file(params[:file_names]) raise if restore_status !=
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
此外,还有几个考虑因素:
- 提出适当的例外情况,而不是零
- 不要拯救所有的例外。拯救那些你正在抚养的孩子
- 像这样试试
def restore_download_delete_file
begin
self.send "#{params[:submit]}"
rescue Exception => e
flash[:error] = "Error with #{params[:submit]}! Please retry."
redirect_to :action => "database_settings"
end
end
def restore
restore_status = restore_file(params[:file_names])
raise if restore_status != 0
flash[:notice] = "File Successfully Restored."
redirect_to :action => "database_settings"
end
def download
download_status = download_file(params[:file_names])
raise if download_status != 0
end
def delete
delete_status = delete_file(params[:file_names])
raise if delete_status != 0
flash[:notice] = "File Successfully Deleted."
redirect_to :action => "database_settings"
end
像这样试试
def restore_download_delete_file
begin
self.send "#{params[:submit]}"
rescue Exception => e
flash[:error] = "Error with #{params[:submit]}! Please retry."
redirect_to :action => "database_settings"
end
end
def restore
restore_status = restore_file(params[:file_names])
raise if restore_status != 0
flash[:notice] = "File Successfully Restored."
redirect_to :action => "database_settings"
end
def download
download_status = download_file(params[:file_names])
raise if download_status != 0
end
def delete
delete_status = delete_file(params[:file_names])
raise if delete_status != 0
flash[:notice] = "File Successfully Deleted."
redirect_to :action => "database_settings"
end
您始终可以将mega方法转换为小型解释器:
def restore_download_delete_file
submit = params[:submit]
if not restore_file(params[:file_names]).zero?
flash[:error] = "Error with #{submit}! Please retry."
elsif submit != "download"
flash[:notice] = "File Successfully #{submit.capitalize}d."
else
return
end
redirect_to :action => "database_settings"
end
但我认为egarcia的方法最有意义;真的有
不需要将所有这些东西混合到一个方法中。共性
实际上是非常小的,所以一个控制器中有四个方法
更有意义:一个行动,一种方法。DRY只是一个指南,不是吗
要不惜一切代价遵循教条。您可以随时将您的mega方法变成一个小型解释器:
def restore_download_delete_file
submit = params[:submit]
if not restore_file(params[:file_names]).zero?
flash[:error] = "Error with #{submit}! Please retry."
elsif submit != "download"
flash[:notice] = "File Successfully #{submit.capitalize}d."
else
return
end
redirect_to :action => "database_settings"
end
但我认为egarcia的方法最有意义;真的有
不需要将所有这些东西混合到一个方法中。共性
实际上是非常小的,所以一个控制器中有四个方法
更有意义:一个行动,一种方法。DRY只是一个指南,不是吗
必须不惜一切代价遵循教条。您正在覆盖现有方法名称,如restore\u file、download\u file等。@Ashish:哪些现有方法?在restore\u file方法中,restore\u file(参数[:file\u names])调用present。这意味着它已经在某个地方定义。好吧,我更改了下载、删除和还原的方法。您正在覆盖现有的方法名称,如restore\u file、download\u file等@Ashish:哪些现有方法?在restore\u file方法中,restore\u file(params[:file\u names])调用present。这意味着它已经在某个地方定义。好吧,我更改了下载、删除和还原的方法。它看起来不错,但我不想在下载场景的任何页面上重定向。在重定向中添加了下载检查它看起来不错,但我不想在下载场景的任何页面上重定向。在重定向中添加了下载检查