Ernesto G
Ernesto G

Reputation: 545

Rails axlsx-rails: ActionController::UnknownFormat

I'm having problems generating an excel report using the axlsx-rails gem since I'm getting Completed 406 Not Acceptable ActionController::UnknownFormat - ActionController::UnknownFormat all the time.

I have a form in the index view with two buttons, one of them submits a search requestshowing the search result as a html table and the other one should allow the user to generate an excel report with the result of the search.

I'm using rails 5.1.6:

gem 'rubyzip', '>= 1.2.1'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git', ref: 'c8ac844'
gem 'axlsx_rails'

In the controller:

orders_controller.rb

def index
  orders = request.query_string.present? ? Order.search(params, 
  current_user) : Order.pendientes

 if params[:button] == 'report'
   @orders = orders
   respond_to do |format| 
     format.xlsx
     response.headers['Content-Disposition'] = 'attachment; 
     filename="Informe_Pedidos.xlsx"'
   end
 else
  @orders = orders.order("#{sort_column} #{sort_direction}").page(params[:page]).per(params[:paginas])
 end
end

I have also added this to the mime_types initializer:

Mime::Type.register "application/xlsx", :xlsx

And I finally have this template: orders/views/index.xlsx.axlsx

EDIT:

The submit buttoin logic in the view:

<%=button_tag type: 'submit', value: 'report', class: 'btn btn-outline-  danger' do %>
  <i class="fa fa-download" aria-hidden="true"></i>
<%end%>

This is the full request and error message:

Started GET "/orders?utf8=%E2%9C%93&f_fin=2018-04-19&f_inicio=&      pendientes=true&favoritos=true&planta=&paginas=&bu                    tton=report&pedido=&sap_cod=&descripcion=&ref=&ot=&desc_ot=&ubicacion=" for  10.0.2.2 at 2018-04-19 17:37:04 +0000
Processing by OrdersController#index as HTML
Parameters: {"utf8"=>"✓", "f_fin"=>"2018-04-19", "f_inicio"=>"",  "pendientes"=>"true", "favoritos"=>"true", "pl                   anta"=>"",    "paginas"=>"", "button"=>"report", "pedido"=>"", "sap_cod"=>"",   "descripcion"=>"", "ref"=>"", "ot"=>""                   , "desc_ot"=>"",   "ubicacion"=>""}
 User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  $1 ORDER BY "users"."id" ASC LIMIT $2  [                   ["id", 1],    ["LIMIT", 1]]
 Completed 406 Not Acceptable in 3ms (ActiveRecord: 0.1ms)

Upvotes: 0

Views: 1377

Answers (3)

MrBobologo
MrBobologo

Reputation: 39

I don't know if you already solved this issue. But if not, try to add "Format: xlsx" to your form.

For example:

<%= form_for User.new, index_path(format: :xlsx) do |f| %>

Upvotes: 2

Petercopter
Petercopter

Reputation: 1258

This is what I've got in a production application right now. I may have written it, but it was awhile ago, so I can't say much about it other than 'it works'. I believe the type param is picking up the mime type from the initializer, which is the same as you have. Hope it helps!

def show
  generated_report = GeneratedReport.find(params[:id])
  send_data(
    generated_report.file_data,
    filename: generated_report.filename,
    type: :xlsx
  )
end

Upvotes: 0

cnnr
cnnr

Reputation: 1307

Try next solution:

...
format.xlsx do
  render xlsx: "index", filename="Informe_Pedidos"
end
...

Upvotes: 1

Related Questions