我如何在 rails 中应用导出到文本功能
How may i apply Export to Text functionality in rails
使用 Rails 4,我想应用导入和导出功能,当我单击“导出到文本”时 link table 中存在的所有 table 数据] 被下载到文本。我已经申请了相同的功能 excel 但现在我想要 text.So 我不知道我该怎么做以及我可以在控制器中写什么。
This is my previous code which i have followed from railcast.
def import_machine_attendance
@emp = Employee.all
respond_to do |format|
format.html
format.csv { send_data @emp.to_csv }
format.xls
end
end
<%= link_to "CSV", import_machine_attendance_machine_attendances_path(format: "csv") %> |
尝试覆盖 to_csv
、
def self.to_csv
attributes = %w{id email name}
CSV.generate(:col_sep => "#") do |csv|
csv << attributes
all.each do |employee|
csv << attributes.map{ |attr| employee.send(attr) }
end
end
end
使用 Rails 4,我想应用导入和导出功能,当我单击“导出到文本”时 link table 中存在的所有 table 数据] 被下载到文本。我已经申请了相同的功能 excel 但现在我想要 text.So 我不知道我该怎么做以及我可以在控制器中写什么。
This is my previous code which i have followed from railcast.
def import_machine_attendance
@emp = Employee.all
respond_to do |format|
format.html
format.csv { send_data @emp.to_csv }
format.xls
end
end
<%= link_to "CSV", import_machine_attendance_machine_attendances_path(format: "csv") %> |
尝试覆盖 to_csv
、
def self.to_csv
attributes = %w{id email name}
CSV.generate(:col_sep => "#") do |csv|
csv << attributes
all.each do |employee|
csv << attributes.map{ |attr| employee.send(attr) }
end
end
end