render with the specified mime type not downloading properly in rails

511 Views Asked by At

I am using following statement, but it is not downloading the excel file instead it downloading the normal text file to the browser.

render 'employees/checkin_report', :dates_arr => @dates_arr, :employees => @employees, :content_type => "application/vnd.ms-excel"

I am having checkin_report.xls.erb file, in employees view folder.

1

There are 1 best solutions below

0
On

If you go to config/initializers/mime_types.rb in your project. You'll see something like this.

#Add new mime types for use in respond_to blocks: 
#Mime::Type.register "text/richtext", :rtf``` 

There you can register new MIME types.

| Short name      | respond_to symbol | Aliases and explanations    |
|:---------------:|:-----------------:| ---------------------------:|
| text/html       | :html :html       | application/xhtml+xml       |
| text/plain      | :text, :txt       |                             |
| text/javascript | :js               | application/javascript,     |
|                 |                   | application/x-javascript    |
| text/css        | :css              | Cascading style sheets      |
| text/calendar   | :ics              | iCalendar format for sharing|
|                 |                   |meeting requests and tasks   |
| text/csv        | :csv              | Comma-separated values      |
| application/xml | :xml              | text/xml, application/x-xml |
| application/    | :rss              | Really Simple Syndication   |
| rss+xml         |                   |format for web feeds         |
| application/    | :atom             | Atom Syndication Format for |
| atom+xml        |                   |web feeds                    |
| application/    | :yaml             | text/yaml--the human        |
| x-yaml          |                   |readable                     |
|                 |                   | Data serialization format   |
| application/    | :url_encoded_form | The default content type of |
| x-www-form      |                   |HTML forms                   | 
| urlencoded      |                   |                             |
| multipart/      | :multipart_form   | Used for HTML forms that    |
| form-data       |                   |contain files, non-ASCII     |
|                 |                   |data, and binary data        |
| application/json| :json             | text/x-json, application/   |
|                 |                   |jsonrequest--                |
|                 |                   | Javascript object notaion   |

I'm sure there are more MIME types you can implement. These are the more common ones.