I have an Rails app (ruby 2.0.0, Rails 4.2.1). I would like to export data to excel using acts_as_xlsx gem.

Here is my controller:

class VulnerabilitiesController < ApplicationController
  before_action :set_vulnerability, only: [:show, :edit, :update, :destroy]
  # GET /vulnerabilities
  # GET /vulnerabilities.json
def index

  @vulnerabilities = Vulnerability.all 

  respond_to do | format |  
  format.html # index.html.erb
  format.json { render :json => @vulnerabilities }
  format.xlsx {
    send_data @vulnerabilities.to_xlsx.to_stream.read, :filename => 'costings.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet"
  }
end
(…)

Here is my model:

class Vulnerability < ActiveRecord::Base
  acts_as_xlsx
end

But when I click on my button:

<%= link_to 'Download', url_for(:format=>"xlsx") %>

I have an error:

Couldn't find all Vulnerabilities with 'id': (all, {}) (found 0 results, but was looking for 2)

Screenshot:

ActiveRecord RecordNotFound

Can anyone help?

1

There are 1 answers

0
hermiti On

Okay, after looking into the issue it appears that the default acts_as_xlsx gem is broken with rails 4.1+. I found a repository that has a patch applied to the original acts_as_xlsx gem to resolve the issue, which can be found here:

https://github.com/straydogstudio/acts_as_xlsx

I updated my gem so that it pointed to the new repository

gem 'acts_as_xlsx',
  :git => "git://github.com/straydogstudio/acts_as_xlsx.git"

Alternatively you could download the zip and extract the folder into the vendor directory (renaming it from acts_as_xlsx-master to acts_as_xlsx). Which is what I did just in case the patched repository disappears.

gem 'acts_as_xlsx',
  :path => "vendor/acts_as_xlsx"

Kudos to straydogstudio for the patch.