When I try to run any command that uses the Twitter API in the ruby console, I get the error NameError: uninitialized constant Twitter.
I have named this twitterFeed.rb because I read that it should not be named twitter.rb. This file is placed in my config/initializers folder. I have ran bundle install already, and the line gem 'twitter', '~> 6.2' is in my gem file.
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
It looks like you're trying to use irb instead of the Rails console to run your code. When you run through irb (or pry), you aren't actually loading the Rails environment, so none of the gems will be available. You can manually require them, but you still won't have access to the Rails environment.
What you want to do instead is use rails console (or rails c for short).
For example with irb, Twitter isn't loaded:
rails_dir » irb
2.2.4 :001 > Twitter
NameError: uninitialized constant Twitter
from (irb):1
from /Users/bbugh/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `<top (required)>'
With rails c, it works just fine:
rails_dir » rails c
Loading development environment (Rails 5.0.1)
2.2.4 :001 > Twitter
=> Twitter
You can take all of those requires out of your initializer - Rails will require the gems automatically by that point. You just need to use rails console when you're doing console work with Rails.
Related
Out of curiosity, where is Rails.application defined? If I do this in irb in a Rails-powered app, I got an error:
$ irb
1.9.3-p327 :001 > require 'rails/application'
=> true
1.9.3-p327 :002 > class App < Rails::Application; end
NoMethodError: undefined method `application' for Rails:Module
I have tried requiring more, like active_support and rails/railtie/configuration but no luck.
The purpose of this is to load a minimal Rails env where I can test an ActiveRecord::Base helper :)
Usually when working with Rails you use rails console instead of IRB. When you run rails console it will boot up your Rails application.
FWIW, Rails.application is defined in railties:
lib/rails.rb
I am getting following error while executing environment.rb
C:\RailsProject>jruby script/delayed_job start
NameError: uninitialized constant RAILS_ROOT
const_missing at org/jruby/RubyModule.java:2647
(root) at C:/RailsProject/config/environment.rb:20
require at org/jruby/RubyKernel.java:1062
(root) at script/delayed_job:3
environment.rb contains following code
# Be sure to restart your server when you modify this file
require "rubygems"
require 'memcache'
if ENV['LOCAL'] == 'Y'
require "bundler/setup"
end
# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
# ENV['Rails.env'] = 'production'
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '3.2.13' unless defined? RAILS_GEM_VERSION
ENABLE_FACEBOOK = false
# Bootstrap the Rails environment, frameworks, and default configuration
Dir["#{Rails.root}/lib/integration/*.rb"].each do | file|
require file
end
# Initialize the rails application
RailsProject::Application.initialize!
I am using rails 3.2.13 version.
Could you please help me on this?
I would like to know if when using pry is possible to have access to the variable app?
As an example, when I try to access the root_path I get the following error:
[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object
Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.
I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
Yes, it works
➜ MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"
I use pry-rails in favor of your overriding of IRB in an initializer.
group :development do
gem 'pry-rails'
end
https://github.com/rweng/pry-rails
Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
You can read more in the pry wiki
I want to test my RESTful resource in Rails:
require 'rubygems'
require 'activeresource'
class Event < ActiveResource::Base
self.site = "http://localhost:3000"
end
events = Event.find(:all)
puts events.map(&:name)
I have installed gem:
gem install activeresource
But when I launch my code I get the error message:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- activeresource (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from service.rb:2:in `<main>'
How can I require ActiveResource?
There is a typo in your require, the following is the correct one:
require 'active_resource'
With bunder you can do this just with
gem "activeresource", require: "active_resource"
Normally gems requires file with name equal to gem’s name, with gem "foo" :require => bar syntax you can specify different file to be included by default.
i think you can do it this way:
require 'rubygems'
gem 'activeresource'
require 'active_resource'
You can also specify which version of activeresource you want by
gem 'activeresource', '=3.0.7'
for example. Or you can omit this line completely, but this way it's cleaner... and it makes sure that you get the right version.
Hope this helps, NoICE
I have a rails 2.3.4 app that I'd like to extend with omniauth (0.1.5). When I install omniauth gem using rvm and place require 'omniauth' in the config.rb file I get the following error:
`gem_original_require': no such file to load -- omniauth (MissingSourceFile)
The tutorials suggest using putting it in the gemfile but I am using rails 2.
When I 'gem list' omniauth is available however.
This has taken a couple of (hair-pulling) days and I am not sure how to proceed.
Am I placing the require in the correct place or is there somewhere else I could put it (aside form the obvious :-))?
Any ideas would be great....
EDIT 1: I tried config.gem "omniauth" in your environments.rb file and got
/home/mcaulejj/explorer/config/environment.rb:10: undefined local variable or method `config' for main:Object (NameError)
EDIT 2: Using RVM I updated all gems but I am still getting the same error.....
I'm exasperated at this point.
Cheers Slothihtype
Try config.gem "omniauth" in your environments.rb file.
EDIT
As per comment,
try:
require File.join(File.dirname(__FILE__), 'boot')
#insert the following here, in your config/environment.rb
if Gem::VERSION >= "1.3.6"
module Rails
class GemDependency
def requirement
r = super
(r == Gem::Requirement.default) ? nil : r
end
end
end
end
Add require 'oa-oauth' in your environment.rb file