Change your action to something like:
format.xls { send_data Kid.to_csv({col_sep: "\t"}, @users) }
You should not have to change anything in your view.
Question
I having the following action:
def users_report
@users = Kid.where(confirmation_token: nil).paginate(:page => params[:page], :per_page => 30)
respond_to do |format|
format.html
format.xls { send_data @users.to_csv(col_sep: "\t") }
end
end
Also I have two views "users_report.html.haml" and "users_report.xls.erb", but when I pressed the button to export to excel I get the file users_report.xls but I can only see objects, but the users_report.xls.erb has his structure all the fields order by columns.
Can anyone help me here, please?
Thanks in advance
UPDATE XLS VIEW
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">Name</Data></Cell>
<Cell><Data ss:Type="String">Surname</Data></Cell>
<Cell><Data ss:Type="String">Email</Data></Cell>
<Cell><Data ss:Type="String">Age</Data></Cell>
<Cell><Data ss:Type="String">School</Data></Cell>
<Cell><Data ss:Type="String">Class</Data></Cell>
<Cell><Data ss:Type="String">Native Language</Data></Cell>
<Cell><Data ss:Type="String">Practised Language</Data></Cell>
<Cell><Data ss:Type="String">Conversations same Native</Data></Cell>
<Cell><Data ss:Type="String">Convserations different Native</Data></Cell>
<Cell><Data ss:Type="String">Message same Native</Data></Cell>
<Cell><Data ss:Type="String">Message different Native</Data></Cell>
<Cell><Data ss:Type="String">Posts</Data></Cell>
<Cell><Data ss:Type="String">Videos watched</Data></Cell>
<Cell><Data ss:Type="String">Clossed/Finished calls</Data></Cell>
<Cell><Data ss:Type="String">Missed calls</Data></Cell>
<Cell><Data ss:Type="String">Connections per Week</Data></Cell>
<Cell><Data ss:Type="String">Nb of foreign friends</Data></Cell>
<Cell><Data ss:Type="String">Nb of friends same country</Data></Cell>
<Cell><Data ss:Type="String">Activation Date</Data></Cell>
<Cell><Data ss:Type="String">Email Parent</Data></Cell>
<Cell><Data ss:Type="String">Parent Activated</Data></Cell>
</Row>
<% @users.each do |user| %>
<Row>
<Cell><Data ss:Type="String"><%= user.name rescue _("No name") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.surname rescue _("No surname") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.email rescue _("No email") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.age rescue _("No age") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.school.name rescue _("No School") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.courses.first.name rescue _("No Course") %></Data></Cell>
<Cell><Data ss:Type="String"><%= sentence_native_languages_of user rescue _("No native language") %></Data></Cell>
<Cell><Data ss:Type="String"><%= sentence_practise_languages_of user rescue _("No practise language") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_native_conversations rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_conversations rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_native_messages rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_messages rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_activity_posts rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_finished_closed_calls rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_missed_calls rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= number_with_precision(user.avg_of_connections_week, precision: 3) rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_friends rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_friends_same_country rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.confirmed_at.try(:strftime, "%d/%m/%Y") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.tutor.email rescue _('No parent email') %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.tutor.confirmed? rescue _('No parent email') %></Data></Cell>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
Solution
Change your action to something like:
format.xls { send_data Kid.to_csv({col_sep: "\t"}, @users) }
You should not have to change anything in your view.
OTHER TIPS
Add the xls mime type to your config/initilizers/mime_types.rb file:
Mime::Type.register "application/xls", :xls