NameError: uninitialized constant Rails with Rails.root in rails 3.2 - ruby-on-rails

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?

Related

NameError: Uninitialized constant Twitter

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.

Rails Server Error from Application.rb file

just started building out a rails project from scratch after months of re-purposing of older code. the new project wont allow me to run rails server from the command line. i'm getting an error reporting the problem is in the application.rb file
rails_projects/platform/config/application.rb:7: undefined method `groups' for Rails:Module (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:28:in `require'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:28
from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:27:in `tap'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.9/lib/rails/commands.rb:27
from script/rails:6:in `require'
from script/rails:6
application.rb file looks like this:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module Platform
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.assets.enabled = true
config.assets.version = '1.0'
end
end
The Rails.groups method was something that was added in Rails 3.1. By the look of it, you're still using Rails 3.0.9. Change the version number for Rails in your Gemfile to 3.1.1 and run bundle update rails to fix this problem.

require other modules in the controller

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

How can you load the Rails environment from CloudCrowd actions?

I'm writing an "action" for CloudCrowd which needs access to the Rails environment (for some ActiveRecord stuff) but the standard means of loading the environment is resulting in fishy errors.
I tried each of the following at the top of my action .rb file:
require(File.join(File.dirname(__FILE__), '../..', 'boot'))
and
require File.expand_path(File.dirname(__FILE__) + "/../../environment")
When I try to start the node I get this error:
»crowd node
Starting CloudCrowd Node on port 9063...
Missing the Rails 2.3.2 gem. Please `gem install -v=2.3.2 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.
And I of course do have the gem installed:
»gem list | grep -i rails
rails (2.3.4, 2.3.2, 2.2.2, 1.2.6)
Nice!
I've actually had some trouble with your RAILS_ROOT path and replaced '../../..' with '../..'. Also, since you've already declared the RAILS_ROOT constant, you could chop off a few things in the environment requirement. Here's my version:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
RAILS_ENV = ENV['RAILS_ENV'] = ENV['RACK_ENV']
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "#{RAILS_ROOT}/config/environment"
# and if you need to import
# anything from lib just go ahead and
require 'my_custom_lib/name_of_file'
end
Somebody from #documentcloud saw my plea and helped me work through it. Had to prefix the action script with this:
RAILS_GEM_VERSION = nil
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../../..'))
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
if CloudCrowd.node?
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.logger = Logger.new(STDOUT)
require File.expand_path(File.join(File.dirname(__FILE__), '../..', 'environment'))
end

How to configure Ruby on Rails with no database?

It would be convenient to use Ruby on Rails for a small website project that has no current need for a database. I know I could create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?
Thanks
For Rails 3 and Rails 4:
Use -O(Capital 'O') or --skip-activerecord option to generate an application without a database.
rails new myApp -O
or
rails new myApp --skip-activerecord
This Answer is reshared from here
For Rails 5:
Use --skip-active-record option to generate an application without a database
Notice the extra hyphen '-' as opposed to previous Rails versions.
rails new myApp --skip-active-record
For an existing Rails 4-7 project, in your config/application.rb file you have the following line:
require 'rails/all' # or `require "rails"' in newer versions
(As reference that line is loading this file)
So instead of load ALL, you must to load each library separately as follows:
# active_record is what we're not going to use it, so comment it "just in case"
# require "active_record/railtie"
# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie"
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie" # Deprecated for Rails >= 7, so add it only if you're using it
require "rails/test_unit/railtie"
# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2
Keep an eye to the comments to know what to load regarding your Rails version.
Also check the following files (in case you have them) and comment the following lines:
# package.json
"#rails/activestorage": "^6.0.0",
# app/javascript/packs/application.js
require("#rails/activestorage").start()
# bin/setup
system! 'bin/rails db:prepare'
# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2
# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false
# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
# test/test_helper.rb
fixtures :all # In case you're using fixtures
# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
Also remove any reference to ActiveRecord::Base in your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rb file.
Uncomment this line in the environment.rb file:
config.frameworks -= [ :active_record, :active_resource, :action_mailer]
In Rails 4 when starting a new project you can use -O or --skip-active-record
rails new my_project -O
rails new my_project --skip-active-record
If you've already created a project you will need to comment
require "active_record/railtie"
from config/application.rb and
config.active_record.migration_error = :page_load
from config/environments/development.rb
If you don't need a database then you probably don't need to have the bulk of Rails. You may want a smaller more customizable framework to work with.
Sinatra is a tiny framework that is great for serving up basic static pages.
But if you insist on using Rails here is an article that will show you how to do just that or here.
For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching
In config/initializers/null_db_adapter_monkey_patches.rb
module ActiveRecord
module ConnectionAdapters
class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
def new_table_definition(table_name = nil, is_temporary = nil)
TableDefinition.new(table_name, is_temporary)
end
end
end
end

Resources