"No such file or directory # rb_sysopen" using OpenURI - ruby-on-rails

I've recently run into a problem using OpenURI. Every open method results in the following error:
"No such file or directory # rb_sysopen".
My code looks simply like the following:
data = open("http://google.ca/")
I noticed the error shortly after adding gem 'nokogiri' to my Gemfile and running bundle install, though I have no indication of whether or not this caused the problem and have since removed the entry with no positive impact on the problem. Any help would be appreciated.

Try to write require 'open-uri' before your code.

I am using Ruby 3.0.1 and a part from the:
require "open-uri"
I have to explicitly call URI.open instead of just open:
data = URI.open("http://google.ca/")
Maybe it is something on new Ruby versions

Related

Local gem class is not found in Rails application

I have written a generator which creates the following ruby file and folder:
app/tests/test.rb
in the test.rb file I have a Test class which looks like this:
class Test < MyCustomModule::MyCustomClass::Base
...
end
Now, I want to use its functionality in one of the show.html.erb files creating new instance like this:
Test.new(...).render(...).html_safe
but I am getting the following error:
uninitialized constant MyCustomModule::MyCustomClass::Base
I have use the following answer to link my gem and my rails application. It seems to work as I am able to use the generator, but the gem module and class are not seen in the rails application.
Could anyone tell how to fix this issue?
I have try to follow the tips posted here but still nothing changed:
Adding config.autoload_paths += Dir["#{config.root}/lib/**/"] in application.rb file
I have created my gem structure looking at CarrierWave gem, so the naming should be correct
I try to disable config.threadsafe! but it is already disabled since config.cache_classes and config.eager_load are set to false in development
DEPRECATION WARNING: config.threadsafe! is deprecated. Rails
applications behave by default as thread safe in production as long as
config.cache_classes and config.eager_load are set to true.
Also, looking at adding-asset-to-your-gems rails documentation, it is said that:
A good example of this is the jquery-rails gem which comes with Rails
as the standard JavaScript library gem. This gem contains an engine
class which inherits from Rails::Engine. By doing this, Rails is
informed that the directory for this gem may contain assets and the
app/assets, lib/assets and vendor/assets directories of this engine
are added to the search path of Sprockets.
So, I have done this, and put my model class file in assets folder, but the result is the same.
The following screenshots demonstrate my real case:
The screenshot below displays my gem file structure
Here you can see how I am loading the gem in my Rails application Gemfile:
gem 'thumbnail_hover_effect', '0.0.3', github: 'thumbnail_hover_effec/thumbnail_hover_effec', branch: 'master'
Then I am using the gem generator a ruby file with a cutstom name in app/thumbnails/test.rb folder with the following code:
class Test < ThumbnailHoverEffect::Image::Base
...
end
and trying to use the Test class gives me uninitialized constant ThumbnailHoverEffect::Image::Base error.
Back in the gem files, these are how the thumbnail_hover_effect file looks like
require 'thumbnail_hover_effect/version'
require 'thumbnail_hover_effect/engine'
require 'thumbnail_hover_effect/image'
module ThumbnailHoverEffect
# Your code goes here...
end
and hoe the image file looks like:
module ThumbnailHoverEffect
#
class Image
...
end
end
From what you've posted here there is no ThumbnailHoverEffect::Image::Base defined. Rails autoloading conventions (which you should not be depending on a gem btw, more on that later) would be looking for this file in thumbnail_hover_effect/image/base.rb, but the directory structure you printed does not have that. Of course you could define the class in thumbnail_hover_effect/image.rb and it would work, but the abridged snippet you posted does not show that. So where is ThumbnailHoverEffect::Image::Base defined?
If it's in thumbnail_hover_effect/image/base.rb then that would indicate the file is not being loaded. You can sanity check this by putting a puts 'loading this stupid file' at the top of thumbnail_hover_effect/image/base.rb. That will allow you to bisect the problem by seeing whether there is a problem with your definition of the class, or whether the problem is with loading the proper files. Debugging is all about bisecting the problem.

rails 3 and rubyzip 1.0.0 uninitialized constant Zip::ZipFile

Im attempting to open a docx file and write back into it using rubyzip 1.0.0 and rails 3.
In my gemfile I have:
gem 'rubyzip'
and the code i'm running is;
module Look
class Generator
def initialize(item)
doc = Nokogiri::XML.parse(item.to_xml)
xslt = Nokogiri::XSLT(File.read("<path_to_xslt_file>.xslt"))
#outxml=xslt.transform(doc)
zip = Zip::ZipFile.open("<path_to_docx_file>.docx")
#outxml
end
end
end
While the #outxml is created correctly (I can manually add it to the docx file and see the results), I can't even begin with creating the zip file because of this...
uninitialized constant Zip::ZipFile
Having checked all the documentation and tried many combinations I'm still completely stumped.
Can anyone please tell me why this won't work?
Thanks.
Just figured this one out by checking the latest documentation. Seems v1.0.0 was only released today so everything I read was out of date.
Anyway, the solution is to use
Zip::File.open

Problems parsing a web-based XML file

I coded a little Ruby script that would parse a remote XML file and extract some data from it using Nokogiri. Now I'm trying to code a more advanced version as a Rails application.
I have my code inside of a controller. It's similar to the code that I used in my Ruby script, however it's not working. I believe the error is because it's trying to load the XML locally rather then externally.
Here is the error that Rails is giving me:
No such file or directory - http://mal-api.com/anime/10?format=xml
Here is a sample of the code in my controller: (I can provide the whole thing if needed, but it's mainly just the default Rails scaffold code.)
def create
require 'nokogiri'
#anime = Anime.new(params[:anime])
doc = Nokogiri::XML(open("http://mal-api.com/anime/#{#anime.mal_id}?format=xml"))
#Title
title = doc.css("anime english_title").inner_html
#Snipped rails scaffold code
end
mal_id is passed in through a form. Nokogiri is added in my Gemfile.
Is there something I'm missing or that I've done wrong?
Any help is appreciated.
By default the open method in ruby is used to open files. If you want to directly open an URL you need to require 'open-uri'. More information can be found in the docs: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html

Uninitialized Constant MyClass::Playback

I'm attempting to use the ruby-alsa gem to provide audio playback on the server. Unfortunately, I keep getting an uninitialized constant MyClass::Playback exception when attempting to do so.
I'm very new to Ruby and Rails, so I'm not sure how to resolve this issue. The following has been added to my Gemfile and I have run a bundle install:
gem 'ruby-alsa'
My controller code looks like this (though I can't even begin to guarantee the validity of the code):
# Test audio playback
file = File.open("sample.wav")
#ALSA::PCM::Playback.open do |playback| # This line is commented out because it didn't work
Playback.open do |playback|
playback.write do |length|
file.read length
end
end
file.close
Update: If I uncomment the following line, I get the same exception (except ALSA is the uninitialized constant):
ALSA::PCM::Playback.open do |playback|
Looking at this briefly, it looks like your Gemfile needs to be:
gem 'ruby-alsa', :require => 'alsa'
It looks as if your commented-out code is correct; you should be using ALSA::PCM::Playback.
Your next problem is that write is an instance method of that class. As shown on the page you linked to, it appears that correct usage might be more like:
ALSA::PCM::Playback.open do |playback|
playback.write do |length|
file.read length
end
end
(Caveat: I know nothing about ALSA or this gem, so I have no idea what the above should do.)
Your code is inside a class so you need to do this:
::ALSA::PCM::Playback.open do |playback|
Note the preceding double colon.
Did you try to require it ?
require 'ruby-alsa'
edit:
Try to require rubygems first
require 'rubygems'
require 'ruby-alsa'

attachment_fu and RMagick

After finally getting RMagick installed on my Mac I have set up attachment_fu according to the tutorial here: http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu&gt when I try and upload a file via the upload form I get around 80 messages like these:
/Library/Ruby/Gems/1.8/gems/rmagick-2.13.1/lib/RMagick.rb:44: warning: already initialized constant PercentGeometry
/Library/Ruby/Gems/1.8/gems/rmagick-2.13.1/lib/RMagick.rb:45: warning: already initialized constant AspectGeometry
/Library/Ruby/Gems/1.8/gems/rmagick-2.13.1/lib/RMagick.rb:46: warning: already initialized constant LessGeometry
/Library/Ruby/Gems/1.8/gems/rmagick-2.13.1/lib/RMagick.rb:47: warning: already initialized constant GreaterGeometry
I did some searching and found that this problem can arise when you require RMagick twice in an application using different casing for the require statement: http://work.rowanhick.com/2007/12/19/require-rmagick-and-case-sensitivity/ I am not requiring it myself, but I was thinking maybe with the config.gem "rmagick" line in my environment.rb file rails might be requiring it.
After the form submits it gives me a validation error of: Content type is not included in the list
I have checked the source for attachement_fu and found the image/png in the list of content types so I don't believe that is the proper error message: http://github.com/technoweenie/attachment_fu/blob/master/lib/technoweenie/attachment_fu.rb
Does anyone have any ideas on how I can get this to work?
If, like us you're using a gem (such as gruff) which requires rmagick as above (and thus you can't really be correcting the case of the require statements), you can configure bundler to load rmagick using the matching case.
E.g. add the following to your Gemfile:
gem 'rmagick', '2.13.1', :require => 'RMagick'
That got us out of a real pickle today.
Thanks for the original post - we were lost until we read it!
Had a similar problem with Paperclip, solved by removing config.gem 'rmagick' line from environment.rb.
Update: The following only works on the Mac. My production server choked on this. Don't use it.
I came across this problem as well. In config/environment.rb I've got:
config.gem 'rmagick'
And it has to be lowercase, otherwise Rails thinks I don't have the required gem installed.
Attachment_fu has a file called rmagick_processor.rb which has the line:
require 'RMagick'
If you change this to lowercase, "require 'rmagick'", the RMagick error messages disappear.

Resources