Two Export buttons to CSV in Rails

北慕城南 提交于 2020-01-17 11:42:08

问题


I'm new to Ruby, I need to add a new button that export different attributes to csv.The old button exports all attributes of the collection. I have this line in controller:

  respond_to :csv, only: :index

I have this in the html view:

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

and I have a file called index.csv.slim, its content:

= collection.to_csv

I have the to_csv method defined and it responds automatically to the export! http://localhost/records.csv

How can I add a new button that responds to different method, should I add another file like index.csv.slim? how can I link them together ? Or at least if I can pass a parameter to the to_csv ? and :

= collection.to_csv(all)

and :

  def to_csv (all = true)

Note: we're using inherited resources and Draper gems. so I don't have actions in controllers. and nothing there in routes file.

Note: using this way


回答1:


solution was to define the index action again:

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

and in the view:

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

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

and inside index.csv.slim

= collection.to_csv(@all)


来源:https://stackoverflow.com/questions/37521492/two-export-buttons-to-csv-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!