Rails 中的两个导出到 CSV 的按钮

Two Export buttons to CSV in Rails

我是 Ruby 的新手,我需要添加一个导出不同属性的新按钮 csv.The 旧按钮导出集合的所有属性。 我在控制器中有这一行:

  respond_to :csv, only: :index

我在 html 视图中有这个:

 = link_to collection_path(format: :csv), tabindex: '-1'
          = fa_icon 'file-code-o', text: 'CSV', class: 'fa-fw'

我有一个名为 index.csv.slim 的文件,其内容:

= collection.to_csv

我定义了 to_csv 方法,它会自动响应导出! http://localhost/records.csv

如何添加响应不同方法的新按钮,是否应该添加另一个文件,如 index.csv.slim?我怎样才能 link 他们在一起? 或者至少如果我可以将参数传递给 to_csv ? 和:

= collection.to_csv(all)

和:

  def to_csv (all = true)

注意:我们正在使用 inherited resourcesDraper gem。 所以我在控制器中没有动作。 路线文件中没有任何内容。

注意:使用这个way

解决方案是重新定义索引操作:

  def index
    respond_to do |format|
      format.csv do
        @all = params[:all].present?
      end
      format.html { super }
    end
  end

并且在视图中:

= link_to collection_path(format: :csv, all: true), tabindex: '-1'

= link_to collection_path(format: :csv, all: false), tabindex: '-1'

和里面 index.csv.slim

= collection.to_csv(@all)