Exporting CSV data from Rails

后端 未结 3 507
孤城傲影
孤城傲影 2021-01-14 10:11

I\'m working to export csv data from rails. I\'m following the tutorial here: http://railscasts.com/episodes/362-exporting-csv-and-excel?view=asciicast

In my control

3条回答
  •  无人及你
    2021-01-14 10:36

    The to_csv is a class method. Meaning its meant to be called like Group.to_csv. You might want to change the method signature to something like Group.to_csv(groups) instead.

     def self.to_csv(groups)
        Rails.logger.info "Hello World"
        CSV.generate do |csv|
          csv << column_names
          groups.each do |product|
            csv << product.attributes.values_at(*column_names)
          end
        end
      end
    

    Then in show

      def show
        # @company is being provided correctly.
        @groups = @company.groups
        render text: Group.to_csv(@groups)
      end  
    

提交回复
热议问题