两个导出按钮到Rails中的CSV [英] Two Export buttons to CSV in Rails

查看:92
本文介绍了两个导出按钮到Rails中的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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 ?如何将它们链接在一起?
或者至少如果我可以传递一个参数到 to_csv
和:

  = collection.to_csv(all)
pre>

和:

  def to_csv(all = true)

注意:我们使用继承资源 Draper 宝石。
所以我没有在控制器中的操作。
并且没有路由文件。



注意:使用此方式

解决方案

解决方案是再次定义索引操作:

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


$ b b

和视图中:

  = 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)


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

解决方案

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)

这篇关于两个导出按钮到Rails中的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆