leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 215 forks source link

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

Closed Anexo closed 9 years ago

Anexo commented 9 years ago

I post this exact issue on Stackoverflow, but didn't get any answer yet.

Please help.

http://stackoverflow.com/questions/26866652/wice-grid-csv-export-not-working-uninitialized-constant-csvwriter

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: http://stackoverflow.com/questions/2139479/write-csv-in-ruby-1-9-and-csvwriter

Thank you all in advance!

leikind commented 9 years ago

what version of WiceGrid are you using?

Anexo commented 9 years ago

I just leave and can't see exactly which version it has till tomorrow, but I think one relatively old, as the changelog stops in 2009.

Thanks for the quick reply, i didn't expect a so great support :)

Anexo commented 9 years ago

Hi, i just checked, and if I'm not wrong in the readme file it's written version 0.4 and the changelog finishes at: 03/04/2009 with "Possibility to add custom lines after and/or before a grid row."

leikind commented 9 years ago

Isn't there a Gemfile?

leikind commented 9 years ago

0.4 is very very very old.

You need to upgrade to 0.6.2, this is the last version for Rails 2. Or just clone the sources from the master branch https://github.com/leikind/wice_grid/tree/master (Rails 3 and 4 are in branch rails3).

Anexo commented 9 years ago

Thank you very much! I will update it and let you know if it works or not!

Anexo commented 9 years ago

Hi again,

I updated to 0.6.2 and in development all works perfect.

When I push the changes to production, It doesn't work.

There is an error starting the server, where it can't find the wice_grid.rb file (although it is in the lib directory).

I supposed I'm doing something wrong, here is what I did:

I uploaded and unzipped the wice_grid files, version 0.6.2.

I run the script for prototype.

Restart the server, and then the web doesn't load and shows the log shows that it can't find the wice_grid.rb error.

I uploaded the files of the 0.4 version and the server starts again.

Do I need to run the script after the server starts? Do I have to run the install.rb inside the wice_grid directory? (I didn't do it because it is not specified in the documentation)

Thank you for your help.

leikind commented 9 years ago

lib directory? Weird. I assume that this is an old Rails 2 app.

The plugin should be here:

$ ls vendor/plugins/wice_grid/
CHANGELOG   init.rb     lib          Rakefile     SAVED_QUERIES_HOWTO.rdoc  test          VERSION
generators  install.rb  MIT-LICENSE  README.rdoc  tasks                     uninstall.rb  wice_grid.gemspec

Can you confirm that this command shows the same on your production server?

Anexo commented 9 years ago

Hi, yes it shows the same! I ment that the wice_grid.rb was inside:

/vendor/plugins/wice_grid/lib

leikind commented 9 years ago

ls config/initializers/wice_grid_config.rb ?

Anexo commented 9 years ago

I can find this file, it starts like this:

if defined?(Wice::Defaults)

  Wice::Defaults::JS_FRAMEWORK = :prototype
  # Wice::Defaults::JS_FRAMEWORK = :jquery

  # Style of the view helper.
  # +false+ is a usual view helper.
  # +true+ will allow to embed erb content in column (cell) definitions.
  Wice::Defaults::ERB_MODE = false

  # Default number of rows to show per page.
  Wice::Defaults::PER_PAGE = 20

  # Default order direction
  Wice::Defaults::ORDER_DIRECTION = 'asc'

  # Default name for a grid. A grid name is the basis for a lot of
  # names including parameter names, DOM IDs, etc
  # The shorter the name is the shorter the request URI will be.
  Wice::Defaults::GRID_NAME = 'grid'
Anexo commented 9 years ago

Here is the error log so you can see whats happening:

Starting Rails with production environment... /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in require': no such file to load -- wice_grid.rb (MissingSourceFile) from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:inblock in require' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in new_constants_in' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:inrequire' from /var/www/trunk/vendor/plugins/wice_grid/init.rb:1:in block in evaluate_init_rb' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin.rb:146:ineval' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin.rb:146:in block in evaluate_init_rb' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/core_ext/kernel/reporting.rb:11:insilence_warnings' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin.rb:142:in evaluate_init_rb' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin.rb:48:inload' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin/loader.rb:38:in block in load_plugins' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin/loader.rb:37:ineach' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/rails/plugin/loader.rb:37:in load_plugins' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/initializer.rb:348:inload_plugins' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/initializer.rb:163:in process' from /usr/local/lib/ruby/gems/1.9.1/gems/rails-2.3.2/lib/initializer.rb:113:inrun'

leikind commented 9 years ago

Ok, I see. It does load vendor/plugins/wice_grid/init.rb, which, if I am looking at the same version, contains just one line: require 'wice_grid.rb'

And it doesn't find it.

You see, dependency management and loading was a f&cking mess before Bundler. I's been years since I had to work with a non-bundlerized app. Probably it it not compatible with non-bundlerized app in production environment.

You have 2 options: try to experiment with requires in the plugin, prepend lib/ to require 'wice_grid.rb' and all other require in the pugin, or just bundlerize the app and let Bundler handle that mess for you.

I'd choose Bundler

http://bundler.io/v1.3/rails23.html

Anexo commented 9 years ago

Ok, I think i get the point, although Im relatively new to rails.

I will take a look at Bundler, but I have two questions:

Will Bundler interfere with other plugins i have installed?

And would it be possible to add this code from 0.4 version initi.rb that works:

#ActionController::Base.class_eval { helper Wice::GridViewHelper }
ActionView::Base.class_eval { include Wice::GridViewHelper }
ActionController::Base.send(:include, Wice::Controller)

To init.rb in order to get the plugin in version 0.6 to work.

leikind commented 9 years ago

You might try. Why don't you first prepend lib/ to wice_grid.rb in require "wice_grid.rb", so that you have require "lib/wice_grid.rb" and other require statements in lib/wice_grid.rb ? I am pretty sure this will work.

If you bundlerize your app you will have to remove all plugins from vendor/plugins and require them via Bundler's Gemfile, with the same version number.

Anexo commented 9 years ago

Ok, I will try it and let you know, thank you very much!

Anexo commented 9 years ago

Hi! The prepend method didn't work :(, i need to work more on it.

Im afraid to ask, but why is the CSV exporting method not working with ruby 1.9.1 and is there an "easy" way to make it work? It is the only feature i want to have right now.

leikind commented 9 years ago

The CSV library in 1.9.x and later versions is different from the one in 1.8.

Have a look here https://github.com/leikind/wice_grid/blob/master/lib/wice_grid_spreadsheet.rb , I use a different API depending on the Ruby version.

I suspect that the version that you had initially didn't have that code and was not ready for ruby 1.9.1

Anexo commented 9 years ago

Thanks, I will have a look!

Anexo commented 9 years ago

Hi,

finally we solve the problem:

the code should look like this:

require 'csv'

module Wice class Spreadsheet #:nodoc:

if RUBY_VERSION == '1.9.1' || RUBY_VERSION == '1.9.2'
  def initialize(name, field_separator)  #:nodoc:
    @tempfile = Tempfile.new(name)
    @csv = CSV.new(@tempfile, :col_sep => field_separator)
  end
  def << (row)  #:nodoc:
    @csv << row
  end
else
  def initialize(name, field_separator)  #:nodoc:
    @tempfile = Tempfile.new(name)
    @field_separator = field_separator;
  end
  def << (row)  #:nodoc:
    CSV::Writer.generate(@tempfile, @field_separator) do |csv|
       csv << row.map(&:to_s)
    end
  end
end

def to_csv  #:nodoc:
  self.close
  File.readlines(@tempfile.path).join('')
end
def path  #:nodoc:
  @tempfile.path
end
def close  #:nodoc:
  @tempfile.close
end

end end

the end tag from the else clause was wrong postition.

Thank you very much for you effort.

leikind commented 9 years ago

I am glad it worked for you