Export from SQL to Excel and perform Download Syntax error: Unexpected ',', expecting end-of-input

Asked

Viewed 46 times

1

I made this Query

MtDispositivo.where("id_conta = 28 or id_conta = 29 or id_conta = 30 or id_conta = 36")

This Query returns me all accounts containing the Ids: 28, 29, 30 and 36.

Could you give me a hint on how to perform the code in Ruby so that it is Exported from SQL to Excel and performed Download?

I did so:

> require 'csv' CSV.generate do |csv| csv <<
> ["headers","describing","the data"] mysql.query("MtDispositivo.where
> "id_conta = 28 or id_conta = 29 or id_conta = 30 or id_conta = 36).each { |row| csv << row }  
end

That is correct?


Updated:

I was able to create the code snippet in Ruby.

def mt_dispositivo_xls

    mt_dispositivo = MtDispositivo.where("id_conta = 28 or id_conta = 29 or id_conta = 30 or id_conta = 36")

    send_data mt_dispositivo.to_xls(:except => [:id_conta]), content_type 'application/vnd.ms-excel', filename: 'mt_dispositivo_xls'
end

Only I’m having this Syntax error when text in the Console.

inserir a descrição da imagem aqui

send_mt_dispositivo.to_xls(:except => [:id_conta]), content_type 'application/vnd.ms-excel', filename: 'mt_dispositivo_xls'
SyntaxError: unexpected ',', expecting end-of-input
...to_xls(:except => [:id_conta]), content_type 'application/vn...

Can you help me with this?

1 answer

0

Doesn’t seem to be a problem in the query by looking at the documentation of the send_data I guess I missed the :, change this:

send_data mt_dispositivo.to_xls(:except => [:id_conta]), content_type 'application/vnd.ms-excel', filename: 'mt_dispositivo_xls'

For this

send_data mt_dispositivo.to_xls(:except => [:id_conta]), content_type: 'application/vnd.ms-excel', filename: 'mt_dispositivo_xls'

However maybe content_type be some older version, apparently today (Rails 5.1.1) is just type, so use it like this:

send_data mt_dispositivo.to_xls(:except => [:id_conta]), type: 'application/vnd.ms-excel', filename: 'mt_dispositivo_xls'

Documentation: http://api.rubyonrails.org/classes/ActionController/DataStreaming.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.