如何在CSV导出中发送超链接(任务URL)

时间:2016-08-09 09:39:30

标签: ruby-on-rails ruby csv hyperlink

我想在CSV文件中显示Task_Url,我已将此查询用于CSV -

user_trackers = TaskTimeTracker.joins(:task).where("(task_time_trackers.created_at BETWEEN ? AND ?)",Date.current.beginning_of_month, Date.current.end_of_month )

@trackers = user_trackers

这是我用过的CSV方法 -

def export_csv
      columns = %w(Date Task Task_URL TimeSpent Log)
      CSV.generate do |csv|
        csv << columns
        @trackers.each do |t|
          csv << [t.created_at,
                    t.task.name,

                    t.time_spent,
                    t.description ]
        end
      end
    end

这是我使用的任务模型。

class Task < ActiveRecord::Base
      TASK_TYPES = { bug: 'Bug', enhancement: 'Enhancement', feature: 'Feature'}
      belongs_to :project
      belongs_to :user
      has_many :task_time_trackers, dependent: :nullify
      validates :task_type,presence: true
end

Task_Time_Tracker模型

class TaskTimeTracker < ActiveRecord::Base
    belongs_to :task
    belongs_to :user
    validates :description,presence:true
    validates :time_spent,presence:true
end

Task_Time_Tracker的架构信息

# == Schema Information
#
# Table name: task_time_trackers
#
#  id          :integer          not null, primary key
#  description :text(65535)
#  time_spent  :float(24)
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  task_id     :integer
#  user_id     :integer

项目模型

class Project < ActiveRecord::Base
  has_many :tasks, dependent: :destroy
  has_and_belongs_to_many :users
end

1 个答案:

答案 0 :(得分:1)

此方法是在控制器或模型中实现的吗?

测试一下:

def export_csv
  columns = %w(Date Task Task_URL TimeSpent Log)
  CSV.generate do |csv|
    csv << columns
    @trackers.each do |t|
      csv << [t.created_at,
                t.task.name,
                task_url(t.task),
                t.time_spent,
                t.description ]
    end
  end
end