Ruby On Rails:如何检查是否下载了csv文件

时间:2022-03-29 23:25:47

I have a download button in my view form, which use an "index" action in the controller

我的视图表单中有一个下载按钮,它在控制器中使用“索引”操作

def index
  respond_to do |format|
    format.html { @some_variables = some_variables.page(params[:page]) }
    format.csv {
      render csv: some_variables, headers: SomeVariable.csv_headers
    }
  end
end

And some def self.to_csv method in my model SomeVariable, in which I generate the CSV file.

在我的模型SomeVariable中有一些def self.to_csv方法,我在其中生成CSV文件。

Is there anyway to check if the download was OK and for example set a flag. if download went OK do something else raise some error end

无论如何都要检查下载是否正常,例如设置标志。如果下载运行正常,则执行其他操作会导致错误结束

I thought about "begin rescue" in a index action, but if there is any other "smarter" implementation sharing it would be more than appreciated.

我在索引动作中考虑过“开始救援”,但如果有任何其他“更聪明”的实现共享,那将是非常值得赞赏的。

2 个解决方案

#1


1  

send_file can be a good alternative to what you're planning to do. You can find more information here How to download file with send_file? and at the API Dock

send_file可以替代您计划的事情。您可以在此处找到更多信息如何使用send_file下载文件?并在API Dock

#2


0  

I figured out a simple solution, I opted for an after_action. after_action is run only if the action before succeeds, and in this case I'm trying to do something if the download was executed i.e if the csv render get a 202. So the if the action index is executed after that I run a method for example updating the downloaded time.

我想出了一个简单的解决方案,我选择了一个after_action。 after_action仅在成功之前的操作运行,并且在这种情况下我尝试执行某些操作,如果执行下载,即如果csv渲染得到202.那么如果在之后执行动作索引我运行一个方法示例更新下载的时间。

class SomeVariableController < ApplicationController

after_action :update_downloaded_time, only: :index, if: -> {current_user.admin?}

def index
  respond_to do |format|
    format.html { @some_variables = some_variables.page(params[:page]) }
    format.csv {
      render csv: some_variables, headers: SomeVariable.csv_headers
    }
  end
end

def update_downloaded_time
  @some_varibale.update_all(downloaded_at: Time.now)
end

#1


1  

send_file can be a good alternative to what you're planning to do. You can find more information here How to download file with send_file? and at the API Dock

send_file可以替代您计划的事情。您可以在此处找到更多信息如何使用send_file下载文件?并在API Dock

#2


0  

I figured out a simple solution, I opted for an after_action. after_action is run only if the action before succeeds, and in this case I'm trying to do something if the download was executed i.e if the csv render get a 202. So the if the action index is executed after that I run a method for example updating the downloaded time.

我想出了一个简单的解决方案,我选择了一个after_action。 after_action仅在成功之前的操作运行,并且在这种情况下我尝试执行某些操作,如果执行下载,即如果csv渲染得到202.那么如果在之后执行动作索引我运行一个方法示例更新下载的时间。

class SomeVariableController < ApplicationController

after_action :update_downloaded_time, only: :index, if: -> {current_user.admin?}

def index
  respond_to do |format|
    format.html { @some_variables = some_variables.page(params[:page]) }
    format.csv {
      render csv: some_variables, headers: SomeVariable.csv_headers
    }
  end
end

def update_downloaded_time
  @some_varibale.update_all(downloaded_at: Time.now)
end