*Suppose we want to download all
icd_code as a csv_file*
Step-1 Create a download button in
given page.
<a href="<%=
download_ic_icd_codes_path(current_ic) %>" class="btn
btn-success">
<i class="fa
fa-download"></i> Download
</a>
step-2
create a action inside icd_code
controller to download
def download
send_data(IcdCode.to_csv(IcdCode.id_asc), type: 'text/csv;
charset=utf-8; header=present', filename: 'IcdCodes.csv')
end
*to_csv is a method which is defined
inside of icd_code model*
step-3
create to_scv method inside icd_code
model.
def self.to_csv(icd_codes)
CSV.generate do |csv|
csv << [ "ICD 9 CODE",
"ICD 10 CODE", "DESCRIPTION", "OFF WORK",
"LIGHT DUTY", "immediate",
"1 week", "2
weeks", "4 weeks", "6 weeks", "8
weeks", "10 weeks", "12 weeks", "16
weeks",
"NOTE", "BODY
PART"
]
icd_codes.each do |icd|
csv << [ icd.name,
icd.icd10, icd.description, icd.off_work, icd.light_duty,
icd.immediate_message,
icd.week_message(1),
icd.week_message(2), icd.week_message(4), icd.week_message(6),
icd.week_message(8),
icd.week_message(10), icd.week_message(12), icd.week_message(16),
icd.note,
icd.get_body_parts
]
end
end
end
step 4
define the route for download action.
resources :icd_codes do
collection do
get :download
end
end
Step-5 Click on dowload buttom.
Comments
Post a Comment