Wice_Grid CSV export not working - Uninitialized constant CSV::Writer

749 views Asked by At

I "inherited" a rails aplication running with ruby 1.8.7 in development.

I have a wice_grid table which I'm trying to export in CSV and in development all goes perfect.

When I push it to production, i get the following error:

uninitialized constant CSV::Writer

The production machine is running Ruby 1.9.1 and from what I read, I suppose the problem comes from there.

I've tried to put:

required 'csv'

In the controller or the model, but nothing happens, development works, production does not.

Here is the controller code:

def index
  require 'csv'
    @service_requests = initialize_grid(ServiceRequest, 
      :name => "solicitudes",
      :order => "created_at" , 
      :order_direction => 'desc',
      :include => [:user, :service],
      :enable_export_to_csv => true,
      :csv_file_name => 'Listado de Solicitudes'
    )
    export_grid_if_requested('solicitudes' => 'service_requests') do
      #Si se pulsa en exportar se exportan todos las celdas de la tabla seleccionada (con filtros aplicados)
    end
end

Here is the part of the view, which calls a partial:

<%= render :partial => 'service_requests' %>

Here is the partial, cropped for making the question not too long:

<%= grid(@service_requests, :show_filters => :always) do |service_request|

 [...]

  service_request.column  :column_name => 'Nombre' , :attribute_name => 'name', :model_class => User do |sr|
    sr.user.name
  end
  service_request.column  :column_name => 'Apellidos' , :attribute_name => 'lastName' , :model_class => User  do |sr|
    sr.user.lastName
  end

 [...]

end %>

I read this thread but didnt help me much: write csv in ruby 1.9 and CSV::Writer

Thank you all in advance!

1

There are 1 answers

0
Jake Worth On BEST ANSWER

Somewhere, that you haven't posted, you are referencing CSV::Writer. This works locally because you're using Ruby 1.8.7, but your production server is using Ruby 1.9.1. CSV::Writer was deprecated with Ruby 1.9.

From the the docs:

# * The old CSV's Reader and Writer classes have been dropped.

Step one is to upgrade your local Ruby to the same version as the server. This will give you the same error locally, which should go away once you find and remove that CSV::Writer.

The CSV docs give examples of how to use the current CSV class to accomplish what CSV::Writer used to do. Here's an example:

# == Writing
#
# === To a File
#
#   CSV.open("path/to/file.csv", "wb") do |csv|
#     csv << ["row", "of", "CSV", "data"]
#     csv << ["another", "row"]
#     # ...
#   end

Upgrading Ruby will probably raise other errors. But Ruby 1.8.7 was retired in 2013 so these are problems you're going to want to fix now rather than later.

Good luck!