Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export to CSV doesn't download directly #326

Open
zarlife opened this issue Sep 5, 2016 · 6 comments
Open

Export to CSV doesn't download directly #326

zarlife opened this issue Sep 5, 2016 · 6 comments

Comments

@zarlife
Copy link

zarlife commented Sep 5, 2016

Plugin version: 3.6.0.pre4
Rails: 4.2.5.1

I followed the instructions outlined here: https://github.com/leikind/wice_grid#csv-export

Even though the export to CSV icon is visible on my index page, it is only clickable after I refresh the page. Then, when clicking on the export to CSV icon, a new tab is opened with the exported CSV in HTML format. If I refresh this page or simply access the link directly, the CSV file will finally be downloaded directly to the computer.

I have spent hours on this problem and have still not found a solution! Any help at all is really appreciated!

My controller:

` def index
@logbook = Logbook.all

@logs_grid = initialize_grid(Logbook, name: 'logs', enable_export_to_csv: true,
  csv_file_name: 'logbook')

export_grid_if_requested('logs' => 'logs_grid')

end`

Just wanted to add that this is a great plugin and I appreciate the work put into it!

@zarlife
Copy link
Author

zarlife commented Sep 6, 2016

Tested on wice_grid 3.6.0.pre3, the same occurs.

My partial has no extra HTML/ERB as well.

This is driving me crazy!!

@zarlife
Copy link
Author

zarlife commented Sep 7, 2016

It seems to be a problem with Turbolinks. When I disable it by removing //= require turbolinks in assets/javascripts/application.js it works.

However, I can't work out a way to make it work with Turbolinks enabled. Adding data-turbolinks = "false" to the tag allows me to no longer refresh the page in order for the button to work. I tried adding the same to the Export button.

So now, when pressing the Export button in a page with tag disabled, the same HTML page with the CSV contents loads up again.

At the moment, the only solution seems to be disabling Turbolinks for the entire project.

Any suggestion is appreciated! Thanks

@miqs1992
Copy link

miqs1992 commented Sep 14, 2016

Hi, I have similar issue and I'm also using wice_grid: 3.6.0.pre4 and Rails: 4.2.5.1
When I render my tables for the first time icon to filter, export to CSV just do literally nothing - no log in terminal or console. After I press name of a column to sort, they become active and I can do all the filters and export.
I tried to remove //= require turbolinks, but it makes icons inactive for all time

controller:

def index
    prefix = params[:prefix] || ''
    items = Item.where("ItemCode LIKE :prefix", prefix: "#{prefix}%")
    @items_grid = initialize_grid(items,
                                  order: 'ItemCode',
                                  include: :item_group_code,
                                  name: 'g1',
                                  enable_export_to_csv: true,
                                  csv_file_name: 'items'
    )
    export_grid_if_requested
  end

_g1_grid.html.erb:

<%= grid(@items_grid, show_filters: :when_filtered) do |g|

  g.blank_slate  do
    'There are no records'
  end

  g.column name: 'Item Code', attribute: 'ItemCode', in_csv: false, auto_reload: true do |item|
    link_to(item.ItemCode, item)
  end

  g.column name: 'Item Code', in_html: false do |item|
    item.ItemCode
  end

  g.column name: 'Description', attribute: 'ItemName', auto_reload: true do |item|
    item.ItemName
  end

  g.column name: 'Group Code', assoc: :item_group_code, attribute: 'ItmsGrpNam', custom_filter: :auto do |item|
    item.item_group_code.ItmsGrpNam
  end

  g.column name: 'Dflt WH', attribute: 'DfltWH', custom_filter: :auto, ordering: false  do |item|
    item.DfltWH
  end

  g.column name: 'Price'  do |item|
    item.LastPurPrc
  end
end -%>

edit:
fixed in #313

@nathanvda
Copy link
Contributor

In wice_grid_processor.js.coffee, when pressing the export link it checks if turbolinks is available and will then visit the link. This is not wanted behaviour when actually we want to download the csv. So I changed this as follows:

exportToCsv : ->
  window.location = @linkForExport

(I now have a local copy of wice_grid_processor which i include instead). I will make a PR for this later.

kreintjes added a commit to kreintjes/wice_grid that referenced this issue Feb 14, 2017
…file method

Fixes issue when trying to download a CSV export in combination with Turbolinks 5
(issue leikind#326).
@kreintjes
Copy link
Contributor

I can confirm the problem and @nathanvda's fix (thanks!). However, I noticed there was also an deprecation warning in combination with Rails 5:

DEPRECATION WARNING: `render :text` is deprecated because it does not actually render a `text/plain` response. Switch to `render plain: 'plain text'` to render as `text/plain`, `render html: '<strong>HTML</strong>'` to render as `text/html`, or `render body: 'raw'` to match the deprecated behavior and render with the default Content-Type, which is `text/html`. (called from block in send_file_rails2 at lib/wice/wice_grid_controller.rb:182)

I fixed the deprecation by replacing send_file_rails2 with Rails' send_file method, which is available since Rails 3, and this also fixes the download problem: #342. I don't think we need @nathanvda's fix any longer, even though it does seem a bit strange to send file downloads through Turbolinks.

kreintjes added a commit to kreintjes/wice_grid that referenced this issue Feb 14, 2017
…file method

Fixes issue when trying to download a CSV export in combination with Turbolinks 5
(issue leikind#326) and fixes Rails 5 deprecation warning concerning `render :text`.
kreintjes added a commit to kreintjes/wice_grid that referenced this issue Feb 14, 2017
@kreintjes
Copy link
Contributor

Whoops. Apparently @nathanvda's fix is needed after all. Probably messed up due to caching. So my PR now fixes two things: this issue and a deprecation warning. Apologies for the confusion!

kreintjes added a commit to kreintjes/wice_grid that referenced this issue Jan 27, 2020
kreintjes added a commit to kreintjes/wice_grid that referenced this issue Jan 27, 2020
kreintjes added a commit to kreintjes/wice_grid that referenced this issue Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants