I use cloud9 ide to use ruby on rails!
I'm testing the gem 'roo' to bring excel file to my DB. before I do it, I wanted to test this gem work.
gem doc : https://github.com/roo-rb/roo
but there is a problem to bring file!
The error message is like this
IOError in MersmapController#index
file ../assets/test.xlsx does not exist
And here is my code!
require 'roo'
class MersmapController < ApplicationController
def index
xlsx = Roo::Excelx.new("../assets/test.xlsx")
#show = xlsx.info
end
end
and in index.erb
<h1> <%= #show %> </h1>
I test this path using my "images.jpg" (the image file)
when I write path of an image file in index.erb it definitely works!!
I tried
xlsx = Roo::Excelx.new("../assets/excel/test.xlsx")
xlsx = Roo::Excelx.new("../../app/assets/test.xlsx")
xlsx = Roo::Excelx.new("../../app/assets/excel/test.xlsx")
......
All the things!!
but finally I couldn't figure out what is the problem...
I appreciate if you help me out!!
You can use Rails.root to get the path name of your file:
xlsx = Roo::Excelx.new(Rails.root.join('app', 'assets', 'excel', 'test.xlsx'))
I remember that once happened to me, check if this solves your problem:
xlsx = Roo::Excelx.new(url_to_file, file_warning: :ignore)
Related
I am attempting to implement Wicked PDF (https://github.com/mileszs/wicked_pdf/) in a Rails 6.1.4 project and am continually getting the following error messages on the console.
My configuration is as simple as it gets. The controller looks like:
class Api::V1::ProductsController < Api::V1::BaseController
def datasheet
#product = Product.active.friendly.find params[:id]
respond_to do |format|
format.pdf {
render "products/datasheet", pdf: "datasheet-#{#product.name}"
}
end
end
end
and the datasheet.haml file looks like:
%html
%head
%body
= "This and that"
The file encoding for the datasheet.haml is UTF-8, but this shouldn't matter because the text contains no special characters.
The Gemfile has both wicked_pdf and wkhtmltopdf-binary.
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
I have executed the command rails generate wicked_pdf and it did generate the wicked_pdf.rb initializer, as expected.
I have tried putting Mime::Type.register "application/pdf", :pdf into the mime_types.rb file, but expect that this wouldn't matter because I am running Rails 6.
The response generated is:
We have another application running Rails 6.1.4 using wicked_pdf in, "ahem...", the exact same way successfully. Obviously something is different, I just cannot find out what...
Any suggestions on what I am overlooking? Your help is much appreciated!
I am trying to unzip a file in my Spree plugin.
Defined the unzipping method in a module which looks like this.
module ImportImages
class Zipper
def self.unzip(zip, unzip_dir, remove_after = false)
Zip::File.open(zip) do |zip_file|
zip_file.each do |f|
f_path=File.join(unzip_dir, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
end
end
FileUtils.rm(zip) if remove_after
end
end
end
I have included the rubyzip gem in my Gemfile.
gem 'rubyzip'
gem 'zip-zip'
When trying to run it, I am getting the following error.
NameError - uninitialized constant ImportImages::Zipper::Zip:
I have tried every solution provided in stackoverflow and other sites. I tried downgrading the version of rubyzip which is 1.2.0 now and add require 'zip' or require 'zip/zip'. Both returned load error.
I have try adding require 'zip/filesystem' to the class. But got
LoadError - cannot load such file -- zip/zipfilesystem
Any solution for this?
Include rubyzip in gem file in this way:
gem 'rubyzip', require: 'zip'
See this question
It's looking for a nested Constant. Change line Zip::File.open(zip) do |zip_file| with below:
::Zip::File.open(zip) do |zip_file|
It should work.
Also make sure you require rubygem/bundle setup. Though in spree it should've already been done.
Babar's answer is correct, but you also need to add require 'zip' in application_controller.rb
I need to stream a TAR file using data from the database in a Rails app.
I know that RubyGems has the TarWriter module that suits my use case perfectly.
The question is, how can I include the module in my Rails app?
I tried to require rubygems/package as follows:
require 'zlib'
require 'rubygems/package'
tar = StringIO.new
Gem::Package::TarWriter.new(tar) do |writer|
writer.add_file("a_file.txt", 0644) do |f|
(1..1000).each do |i|
f.write("some text\n")
end
end
writer.add_file("another_file.txt", 0644) do |f|
f.write("some more text\n")
end
end
tar.seek(0)
gz = Zlib::GzipWriter.new(File.new('this_is_a_tar_gz.tar.gz', 'wb'))
gz.write(tar.read)
tar.close
gz.close
The require failed on the controller. I tried to adding gem 'rubygems/package' on my Gemfile but it didn't work.
It's a noob question, how can I use RubyGems modules from a Rails app?
You should specify full path to module: Gem::Package::TarWriter and add require 'rubygems/package' to your controller.
I have a pdftotext.rb file in /lib and the code is
module Pdftotext
require 'rubygems'
require 'docsplit'
class << self
def convert
Docsplit.extract_text("hello.pdf")
end
end
end
I have the hello.pdf file in the /assets folder and I tried "assets/hello.pdf" but it keeps telling me Error: Couldn't open file '/assets/hello.pdf': No such file or directory.
How can I get the right path to get the file to be converted?
By the way I am using rails 3.2.1, thanks.
Do you mean it is in RAILS_ROOT/assets/hello.pdf?
You should use File.join to get at the file. Like this:
module Pdftotext
require 'rubygems'
require 'docsplit'
class << self
def convert
Docsplit.extract_text(File.join(Rails.root, "assets", "hello.pdf"))
end
end
end
Using "/assets/hello.pdf" will try to get it from the file system root.
I'm using the .foreach method from the Ruby CSV library and I need help in finding a path to a file within my rails application.
CSV.foreach("path/to/file.csv") do |row|
# use row here...
end
To upload CSV files I'm using the Paperclip gem which has a method .url for the file location:
CSV.foreach(CsvUpload.last.csvfile.url) do |row|
#more code
end
CsvUpload Load (0.2ms) SELECT `csv_uploads`.* FROM `csv_uploads` ORDER BY csv_uploads.id DESC LIMIT 1
No such file or directory - /system/csvfiles/17/original/uploadthis.csv?1305217588
The actual path of the file is: /Users/boris/projects/chaggregator/public/system/csvfiles/17/original/uploadthis.csv?1305217588
Is there a Rails method for getting the full path?
You need to include the "/Users/boris/projects/chaggregator/public" portion. Paperclip includes a path method to give this to you:
CSV.foreach(CsvUpload.last.csvfile.path) do |row|
#more code
end