【问题标题】:export data between two dates into a csv file将两个日期之间的数据导出到 csv 文件中
【发布时间】:2021-09-14 18:52:50
【问题描述】:

我有index.html.erb 看起来是这样的:

当用户按下“应用”按钮时,我想将表中的所有日期导出到 csv 文件中(并保存)。

我在我的reports_controller.rb 下一行定义了:

require 'csv'

我通过 ajax 将所有参数传递到控制器(到函数更新)。

现在:

start_day = 用户选择的开始日期

.

.

.

end_year = 用户选择的结束年份

在我的更新功能中,我尝试创建 csv 文件。为了在两个日期之间在我的表中进行搜索,我创建了两个变量:start_date 和 end_date。

start_date = YYYY-MM-DD(而:YYYY 是 start_year,MM 是 start_month,DD 是 start_day)。

end_date 类似于 start_date(但有:end_year、end_month 和 end_day)

这是我的更新功能:

def update
    @start_day = params[:start_day]
    @start_month = params[:start_month]
    @start_year = params[:start_year]

    @end_day = params[:end_day]
    @end_month = params[:end_month]
    @end_year = params[:end_year]

    # the format of start_date and end_date will be: "YYYY-MM-DD"
    @start_date = @start_year + "-" + @start_month + "-" + @start_day
    @end_date = @end_year + "-" + @end_month + "-" + @end_day

    # get all the transactions between start_date to end_date
    @transactions = BillingTransaction.find(:all, :conditions =>["date(created_at) BETWEEN ? AND ? ", @start_date, @end_date])

    # create the csv file
    csv_string = CSV.generate do |csv|
        # insert the headers
        csv << ["transaction_type", "payment_method"]
        # run all over the transactions
        @transactions.each do |user|
            # each of transactions is inserted into the csv file
            csv << [BillingTransaction.transaction_type, BillingTransaction.payment_method]
        end
    end         

    # save it as 'BillingTransaction.csv'
    send_data csv_string,
    :type => 'text/csv; charset=iso-8859-1; header=present',
    :disposition => "attachment; filename=BillingTransaction.csv" 
end

【问题讨论】:

    标签: ruby-on-rails csv export


    【解决方案1】:

    首先它不应该在update方法中,因为它违反了RESTful原则。您可以创建一个自定义方法,该方法可以称为 transactions 或类似名称。

    因为它不会修改任何内容,并且会生成 CSV,因此它可以用作 GET 方法。当然,您需要修改路由以使此方法可见。

    从参数创建Date 对象也是一个好主意,例如:

    @start_date = Date.new(@start_year.to_i, @start_month.to_i, @start_day.to_i)
    @end_date = Date.new(@end_year.to_i, @end_month.to_i, @end_day.to_i)
    

    而不是构建字符串。

    each 块中,您应该添加实际值:

    @transactions.each do |transaction|
      # each of transactions is inserted into the csv file
      csv << [transaction.transaction_type, transaction.payment_method]
    end
    

    最后,对于那些不想在视图中使用的变量,您不需要使用实例变量(以@ 符号为前缀)。

    所以这里什么都不需要前缀@

    【讨论】:

    • 谢谢!!!我让你建议我的所有内容并使方法事务可见,方法是:resources :reports do collection do get :tarnsactions end end 并将 ajax 方法更改为: $.ajax({ url: '/reports/transactions', type: 'GET',数据:{start_day: $(".start_day").attr("value"), start_month: $(".start_month").attr("value"), start_year: $(".start_year") .attr("value"), end_day: $(".end_day").attr("value"), end_month: $(".end_month").attr("value"), end_year: $(".end_year" ).attr("值")} });现在做什么?我按下了“应用”,没有任何反应。
    • 查看日志是否有浏览器发出请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多