To include httparty in my rails 4.0.0 app, in my gemfile I wrote:
gem 'httparty'
and then ran bundle install
Next in my application.rb file, I inserted this:
module myApp
class Application < Rails::Application
### ---
config.gem "httparty"
### ---
end
Now when I load rails c and do a require "httparty", I get false
What am I doing wrong? How do I load httparty in my rails app?
It's already loaded by Rails. You also don't need the config.gem in application.rb
Try using HTTParty from the console. It should just work.
Related
I'm just looking into Nokogiri and was thinking about using it in my app, but apparently when I do bundle install (without gem 'nokogiri') it's already "Using nokogiri 1.6.7.1".
When I add gem 'nokogiri' in my Gemfile, there's no "installing..." So, is nokogiri already pre installed in Rails? If so, do I still have to require these:
require 'nokogiri'
require 'open-uri'
Where do I put this? Within my controller? or application.rb?
This is my application.rb looks like
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
if Rails.env.test? || Rails.env.development?
CONFIG = YAML.load(File.read(File.expand_path('../aws.yml', __FILE__)))
CONFIG.merge! CONFIG.fetch(Rails.env, {})
CONFIG.symbolize_keys!
end
module App
class Application < Rails::Application
config.middleware.use Rack::Pjax
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
end
end
Nokogiri is required by another gem (rails-dom-testing). So it's already installed.
And you don't need to write require 'nokogiri' statement. Because Rails uses Bundler to manage dependencies and load gems. Nokogiri will be already loaded.
We are upgrading Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:
s.add_dependency 'activerecord-session_store'
and added following in initializers/session_store.rb under dummy:
Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'
then, we did bundle install. When we ran:
bundle exec rails generate active_record:session_migration
There is the error from the gem's generator:
/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).
We move the gem into engine's Gemfile and same error. Why the SessionStore is still not initialized?
Update
In engine's engine.rb under lib, the session table is pointed to:
initializer "Authentify.add_middleware" do |app|
ActiveRecord::SessionStore::Session.table_name = 'authentify_sessions'
app.middleware.use ActiveRecord::SessionStore
end
The setup works for Rails 3.2.
If you were not using the default table name for sessions, set:
ActiveRecord::SessionStore::Session.table_name = 'your_old_session_table'
in config/application.rb.
Additional configuration.
What we did is to add gem 'activerecord-session_store' to engine's Gemfile in addition to the .gemspec. The error disappeared.
When I run test.rb (see below) as a separate ruby file from within my rails project it works fine but when I wrap it as a module to be called from a controller it gives me:
LoadError (no such file to load -- eventmachine):1 in 'ModuleTest'
The gem is installed (sudo gem install event machine and bundle install) and added to the gem file (gem 'eventmachine').
Could someone please advice on what it is that I'm missing?
Separate file (called through: $ ruby lib/test.rb):
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'fiber'
def doStuff
end
doStuff
Module:
require 'eventmachine'
require 'em-http'
require 'fiber'
module ModuleTest
def doStuff
end
end
Controller:
require 'moduletest'
class MyController < ApplicationController
doStuff
end
Add
gem 'eventmachine'
Into your Gemfile. Then execute bundle install.
If you want to use a new gem in your rails project, you need to add it into Gemfile. To learn more, visit http://gembundler.com/
gem install rubyoverflow
irb
> require 'rubyoverflow'
=> true
But:
require 'rubyoverflow'
include Rubyoverflow
class QuestionsController < ApplicationController
def question_by_tag
ruby_q = Questions.retrieve_by_tag('ruby')
Get error:
LoadError in
QuestionsController#question_by_tag no
such file to load -- rubyoverflow
Rails.root:
D:/artefacts/dev/projects/stack
app/controllers/questions_controller.rb:1:in
`'
This error occurred while loading the
following files: rubyoverflow
Is there any special rules to import moduled in the controller?
why do you use both require and include? include Rubyoverflow will be enough
UPD
For gem you should add it into your Gemfile (Rails 3.x) or config/environment.rb (Rails 2.x)
# Gemfile
gem "rubyoverflow"
# environment.rb
config.gem "rubyoverflow"
Then run bundle for Rails 3.x and rake gems:install for Rails 2.x
I have a very simple controller:
require 'net/ssh'
class MyController < ApplicationController
def foo
render :text => 'bar'
end
end
But when I request http://server:3000/my/foo I get:
MissingSourceFile in MyController#foo
no such file to load -- net/ssh
The gem is installed
> gem list net-ssh
*** LOCAL GEMS ***
net-ssh (2.0.11)
Also, I tried require 'net/ssh' in IRB, and it works.
MyController works fine on Windows, but fail on Ubuntu.
What can be wrong?
In a project I am working on we have used the config/environment.rb file to hold the gem require stuff. So
Rails::Initializer.run do |config|
# ...
config.gem 'net-ssh'
config.gem 'daemons'
config.gem 'slave'
config.gem 'vpim'
config.gem 'json'
# ...
end
I think you will require 'net-ssh' rather than 'net/ssh'. However we did run into a problem where have a hyphen in the name of the gem led to failures. Then we had to do
config.gem 'Ruby-IRC', :lib => 'IRC'
so that version maybe required for you. So that would be
config.gem 'net-ssh', :lib => 'net/ssh'
in case of rails 3.0
this solution if OK.
add this in the yourapp/Gemfile,
gem 'net-ssh
This may help:
Rails Gem Dependencies and Plugin Errors
This is also worth watching:
Railscasts: Gem Dependencies
In my case, since it's a stand alone ruby app, I only needed to require rubygems.
You can also use Dr Nic's ''gemsonrails'' and load vendored gems as plugins, check:
http://gemsonrails.rubyforge.org
I think, the original problem was that I used normal user instead of root:
$ gem install net-ssh
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/alex/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
So, I guess, rails could not find this gem.