gem not available, even after adding to gemfile and running bundle install - ruby-on-rails

I'm making a simple task where I need to parse an XML Http response, all the http is working fine, and I have my xml string....
I'm trying to use the xml-simple gem.
I've gem install xml-simple
I've also added gem 'xml-simple' to the gemfile
Ran bundle install with success
but when I try to require 'xml-simple' in my rake task it fails saying no such file to load -- xml-simple...
What am I missing???

Bundler tries to load the gem by using the gem name as the require path (i.e. require 'xml-simple'). But in the case of the xml-simple gem, the path is xmlsimple, not xml-simple.
So in your Gemfile, use this instead:
gem 'xml-simple', :require => 'xmlsimple'

Gems sometimes have a different require path than the gem name, try either:
gem 'xml-simple', :require => 'xml/simple'
or
gem 'xml-simple', :require => 'xmlsimple'
or whatever the correct require path is to specify

Related

"Attaching" a gem to Rails to prevent "require" everywhere?

The name of a gem is not the same as the module. Right now I need to require it and include in various types of files, such as controllers and models. This is amounting to many requires which I don't have to do for other gems. It's a gem I can update. Is there a way the gem needs to be configured so it is "attached" to Rails, and if so, where may I find instructions to do this?
UPDATE: using required: "name-of-module" in Gemfile removes need for require everywhere. Still wondering, if gem could be configured to not require this in Gemfile?
In the Gemfile, you can do these things:
# Require a different file than the gem's name
gem 'foo', require: 'bar'
# Install but not require anything.
# You need to manually require the gem somewhere.
gem 'foo', require: false
You can still add version and platform specification if you want.
Real-world examples are ActiveSupport and rspec:
gem 'activesupport', '~> 5.2', require: 'active_support/all'
gem 'rspec', '~> 3.1', group: :test, require: false
You need to specify which files you want Ruby to load for you, so you either need specify the gem in your Gemfile (and run bundle exec ...) or put require in the right place(s) in your code. There's no way around that.
If it's a gem that you work on at the same time as you use it in another project, then you can specify a path to the gem. Like
# Gemfile
source "https://rubygems.org"
ruby '2.6.1'
gem 'my_gem', path: '/home/user/Development/my_gem'
This way, you can change your gem and use it directly without having to build and install it.

Rails doesn't load actionform gem from Gemfile

My Rails 4.2 app fails to load some GitHub hosted gem (installed via bundler v1.8.2). I am using rvm 1.26.10 (master)
In my Gemfile,
gem 'simple_form', '~> 3.1.0'
gem 'actionform', :github => 'rails/actionform'
Bundler install them in different location:
$ bundle show simple_form
/Users/me/.rvm/gems/ruby-2.1.5#my_app/gems/simple_form-3.1.0
$ bundle show actionform
/Users/me/.rvm/gems/ruby-2.1.5#my_app/bundler/gems/actionform-4a858fecf4c2
Rails never load the actionform gem.
After inserting the line //= require action_form to my app/assets/javascript/application.js file, this error comes
Sprockets::FileNotFound at / couldn't find file 'action_form'
However, the action_form.js file exists in the gem file.
Moreover, when i try to reproduce the readme example, i got this error
NameError at /conferences/new uninitialized constant ActionForm
require 'bundler/setup' is in boot.rb
Any advise about this issue?
Thanks!
The problem is a mismatch between the gem name and the file inside the gem. Because the gem is named 'actionform', Bundler will try to require 'actionform', however the file is actually called action_form.
You can tell Bundler the right file name with this syntax:
gem 'actionform', :github => 'rails/actionform', :require => 'action_form'
Note that it is normal for gems from git sources to be installed into a different location than gems installed from gem servers. It has nothing to do with this problem.

Understanding Gemfile (Rails): "require mongo" unnecessary if "gem 'mongo'" is already included?

This is our Gemfile.
Is the line require mongo redundant since gem 'mongo' is already included?
If not, what is the purpose of require mongo?
We're on Rails 3.
Thanks!
source 'http://rubygems.org'
require 'rubygems'
require 'mongo'
gem 'rails', '3.0.6'
gem 'mongo'
gem 'mongo_mapper'
gem 'fastercsv'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
#gem 'sqlite3'
gem 'mysql'
gem 'whois'
You shouldn't put require statements in your Gemfile. This is also true for the require 'rubygems' on the line before.
What require does, is what it always does: load the gem. The Gemfile is loaded when you run bundle install. If you try to load a gem before bundle install has run, the gem might not be installed yet.
Gems specified in your Gemfile are required by Rails by default too, by the way.

Creating a Gemfile with RVM

I have a rails 2.3.8 app. I just installed RVM, and apparently things are working ok (I created a very simple rails project to check that rails and mysql were running ok).
I am now trying to create a Gemfile... but am facing a problem with Thinking-Sphinx. Here is the current version of my Gemfile:
source :rubygems
source "http://rubygems.org"
source :rubyforge
source "http://gems.rubyforge.org"
source :gemcutter
source "http://gemcutter.org"
gem 'SystemTimer', :platforms => :ruby_18
gem "rails", "2.3.5"
# make sure to run "bundle config build.mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config" in terminal before running "bundle install" as per documentation in http://gembundler.com/man/bundle-config.1.html
gem "mysql"
gem "tlsmail"
gem 'delayed_job'
gem 'will_paginate', '~> 2.3.11'
gem "chronic"
gem "nokogiri"
gem "sphinx", '0.9.10.2122'
gem 'thinking-sphinx', '< 2.0.0', :require => 'thinking_sphinx'
gem 'ts-delayed-delta', :require => 'thinking_sphinx/deltas/delayed_delta'
gem "mini_magick"
gem "faker"
gem "populator"
gem "rspec"
gem "geokit"
gem "geokit-rails"
And my Rakefile is:
require 'thread'
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'thinking_sphinx/tasks'
require 'thinking_sphinx/deltas/delayed_delta/tasks'
begin
gem 'delayed_job', '~>2.0.3'
require 'delayed/tasks'
rescue LoadError
STDERR.puts "Run `rake gems:install` to install delayed_job"
end
When I run
rake ts:config
I get the following error message:
rake/rdoctask is deprecated. Use rdoc/task instead (in RDoc 2.4.2+)
Sphinx cannot be found on your system. You may need to configure the following
settings in your config/sphinx.yml file:
* bin_path
* searchd_binary_name
* indexer_binary_name
For more information, read the documentation:
http://freelancing-god.github.com/ts/en/advanced_config.html
Generating Configuration to /Users/alex/Sites/myproject/config/development.sphinx.conf
rake aborted!
uninitialized constant ThinkingSphinx::Deltas::DelayedDelta
Tasks: TOP => ts:config => thinking_sphinx:configure
(See full trace by running task with --trace)
I've been trying different things for a couple hours but can't find how to solve the problem... would love some help!
Try bundle exec rake ts:config
This will use the version you specified in your Gemfile.
My advice is to not use RVM. It is a good idea, but poorly implemented. If you need multiple ruby versions like me it is far less of a hassle to just create aliases for each one.
RVM causes issues from time to time, using aliases never does.

Launching RoR server for an existing app gives error

I just made my first setup of RoR, and creating a new application works fine. But when I want to run the rails server I get the following error:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.3/lib/delayed/yaml_ext.rb:30:in 'remove_method': method 'to_yaml' not defined in Class (NameError)
I ran the "bundle install" in the app directory and installed all the necessary files (with some problems however, but I excluded the gems with problems in the gemfile) and the last bundle install said that I have successfully installed all the needed packs.
I'm lost and I can't find a similar error on the internet. Can you help me?
EDIT: I forgot to mention that I'm not having a problem with a new application. It's running an existing one (that I didn't build, but works fine for others) that the error is related to. Here's the gemfile for that app:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'mysql'
gem "haml", ">= 3.0.0"
gem "haml-rails"
gem 'simple_form'
gem 'delayed_job'
#gem 'auto_crawlers'
gem 'will_paginate', '~> 3.0.beta'
group :test do
gem 'factory_girl_rails'
gem 'mocha'
end
group :development do
#gem "nifty-generators", "0.4.3", :git => "git://github.com/JonasNielsen/nifty-generators.git"
gem 'fastercsv'
end
gem "mocha", :group => :test
Do you think the error is because I left out the two gems with "#" ? Those were causing problems at first, and I don't think I need them to test some minor changes in the app (some views that I have to modify)
The issue is being described, and supposedly fixed here: https://github.com/collectiveidea/delayed_job/issuesearch?state=open&q=yaml#issue/194
Try this from the command line: irb -rubygems -r delayed_job and then from your bundled directory bundle-exec irb -rubygems -r delayed_job which will show if there is a difference between your system gems and your bundled setup - you might see an error in one or both attempts to run IRB.
If there is an error using bundle-exec but not with your system gems then it's a bundler issue. If not, are you sure the app is designed to function under Ruby 1.9? It looks like to_yaml isn't available at the point DJ is required, which implies it probably needs a require "yaml" somewhere.

Resources