File does not exist, while using roo in Ruby-on-rails - ruby-on-rails

I am developing a small Ruby-on-rails application. I am using 'roo' gem to open an excel file.
But rails throws an IO error while attempting to open the file. It says file does not exist.
It works fine in irb. My development machine is windows. Here is my code
file ="#{RAILS_ROOT}/public/data/import.xls"
file.gsub!("\\","/")
workbook = Excel.new(file)
Any help is appreciated
thanks,
Abhilash

It would be worth using the File class here rather than creating the path and gsubbing file separators. For example:
file = File.join(RAILS_ROOT, 'public', 'data', 'import.xls')
I'm pretty sure you don't need to worry too much about using backslashes for file separators though in Windows (I've stopped developing on windows though so can't test).
You can then test whether ruby thinks the file exists by doing File.exists?(file) prior to doing anything roo-specific.
Also, are you running your rails app and console as different users? That might cause some permissions problems in one but not the other.

Related

Ruby on Rails application - No such file to load

I'm trying to bring up an old Ruby on Rails application from a crashed server, and when I try to access it via the browser, I get this screen:
For copy-and-paste / searchability, here's the text:
Ruby on Rails application could not be loaded
---------------------------------------------
A source file that the application requires, is missing.
It is possible that you didn't upload your application files correctly. Please check whether all your application files are uploaded.
A required library may not installed. Please install all libraries that this application requires.
Further information about the error may have been written to the application's log file. Please check it in order to analyse the problem.
Error message:
no such file to load -- active_record/locking/optimistic
Exception class:
LoadError
Application root:
/srv/---REDACTED---/current
Backtrace:
---the backtrace---
That first line mentioned in the backtrace is:
include Locking::Optimistic, Locking::Pessimistic
So it's looking for active_record/locking/optimistic, which I found in a variety of other locations:
/data/srv/---REDACTED---/shared/bundle/ruby/1.8/gems/activerecord-3.0.20/lib/active_record/locking/optimistic.rb
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.20/lib/active_record/locking/optimistic.rb
/var/lib/gems/1.8/gems/activerecord-3.0.20/lib/active_record/locking/optimistic.rb
My problem is that I have no idea how to tell Ruby where to go look for the files it thinks are missing. I am not a Ruby person and I don't know how Phusion Passenger runs things, but I know most languages have concepts of "paths" to search and I'm assuming I just need to set a path somewhere.
Any ideas?
I ended up manually copying over the missing files from the
/data/srv/---REDACTED---/shared/bundle/ruby/1.8/gems/activerecord-3.0.20/lib/active_record/locking/optimistic.rb
...to the gem that was reporting the missing file, since it seemed to be the same gem and same version.
Running the bundle install didn't change anything, for some reason.

Extraction directory of Ruby application via OCRA

I'm using OCRA (One Click Ruby Application) to create a portable Ruby on Rails application. I encountered an error that goes like this:
missing helper file helpers/application_helper.rb helper.rb
This error is related to case sensitivity issue with Ruby 2.2, where using C:\User\Sites would cause an error.
I upgraded ruby to 2.3, but the error retains. I noticed that some of the characters were changed as well (dot to dash, ocr.xxx becomes ocr-xxx).
Is there a way to specify to which directory OCRA should be extracting the code? I was thinking of placing it somewhere other than the TEMP directory, which seems to be an issue for me.

Ruby Character Encoding Confusion When Reading Same File In Different Environments

I have a Rails application that accepts file uploads of CSV files. When developing the feature locally on my Mac, I received an "invalid byte sequence in UTF-8" error when trying to parse the uploaded file (using Ruby's standard library CSV).
So after doing some research and reading some answers to similar questions on StackOverflow, I tried using a gem to sniff out the character encoding (namely CharDet), and then when opening the file via the CSV library, I would specify the encoding. And this solved all my problems, and life was good.
content = File.read(fullpath)
self.file_encoding = CharDet.detect(content)['encoding']
CSV.table(fullpath, :encoding => file_encoding, :header_converters => :downcase).headers
But then I deployed this code to the production Linux environment, and again with the "invalid byte sequence in UTF-8" errors. What a mystery (to me anyway)! After quite some time trying to resolve the error, I tried removing the code that specified the encoding upon opening the file. And miraculously it fixed the problem on production, but now local Mac development is broken.
Keep in mind, that in both cases I'm uploading the same file using the same browser. Does anyone have any insight on what is going on here?
By the way, versions of ruby are close, but not the same. The Mac is ruby 1.9.3-p0, and the Linux server is 1.9.2-p180. The app is Rails 3.2.6.
A few thoughts:
Have you confirmed the encoding of the file that you're uploading?
Have you tested with 1.9.2-p180 on your Mac, as Frederick Cheung suggested?
Have you tried outputting the results of CharDet.detect on each platform to see what the encoding of the received file (as opposed to the uploaded file) is? I wonder if some configuration is different between Apache on Linux and WEBrick on your Mac?
Are you using the same version of CharDet on both platforms? What libraries does it use (e.g. iconv), and are they the same version on both platforms?
I'm not aware of any differences in behavior with regard to encoding between 1.9.2 and 1.9.3, but I haven't specifically researched it either. It could also be a difference in the configuration of the MRI build.

Getting Xapian running in Ruby on Rails app: Couldn't detect type of database

I am trying to get a Ruby on Rails application running on Ubuntu. It utilizes Xapian in order to search for documents. I already installed the xapian-full Gem in version 1.1.3.4 as instructed by the Gemfile and I created the directory files/default where the Xapian database will probably be.
database = Xapian::Database.new('files/default');
As soon as the code runs into this line, there is an error:
IOError in SearchController#index
DatabaseOpeningError: Couldn't detect type of database
Do I need to initialize the database or something? I looked the Xapian Docs and I searched for the error message on the internet, but none of this really helped.
(Writing this answer with knowledge of Xapian, but not the xapian-full Gem, so it's possible some details may be wrong - but the error comes from Xapian, so I'm pretty sure this is on the right lines.)
The error is because you created the directory files/default. Instead, just create the files directory, and ensure the process running Xapian has permission to write to that directory.
Why does Xapian raise an error here? Well, it's because Xapian databases consist simply of a directory containing a special set of files. When Xapian::Database.new is called, it checks if the database already exists before creating a new one. In the default opening mode, if the database directory already exists, it assumes that it shouldn't overwrite whatever's there with a new database, so it tries to open the existing database. Because the directory is just empty, this throws the error you see.

What does "dispatches" files in rails src folder mean?

I just look up at rails sources and find folder named "dispatches". There is four file in it. I want to know purpose of this files. I know I use this files on my production server, but I never used to think of their purpose. I know there is something about attaching Rails app to Apache server. On my production server rails appname command add this files to public folder automatically. Can I set up this behavior on my development machine?
The rails dispatcher is the entry point for a rails application and is used to bootstrap the environment.
They have a long history and in a lot of ways they are almost obsolete. In days gone by rails apps used to be powered using cgi or fastcgi, which was a way for the webserver to communicate with a rails process. The boot process would be initiated by dispatch.fcgi or dispatch.cgi. Nowadays people are more likely to use apache/nginx+passenger or apache/nginx+mongrel/thin. (Does anyone still use lighttpd?)
I'm a little fuzzy on how dispatch.rb is used, but I think it's used by upstream rails servers such as mongrel/thin to bootstrap the rails process. However, now that rails is rack compatible I'm not entirely sure if that has changed.
You don't need to pay the dispatch.* files any attention.
I hope this helps.

Resources