I'm trying to add an entire folder to the JRuby 1.5 classpath for my Rails app. The JRuby Wiki suggests the following: "... add the config directory to the JRuby classpath in config/environment.rb:"
$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, 'config'))}/"
That doesn't seem to work for me. It doesn't matter whether I put that before, after or inside of the Rails::Initializer.run block. No matter what, I get:
/home/sean/src/sbruby/seo/config/environment.rb:45:NoMethodError: undefined method `<<' for nil:NilClass
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController
For example, I'm trying to add a folder under RAILS_ROOT called resources/foobar, so I added the following to environment.rb:
$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, "resources", "foobar"))}/"
Same error.
What is the right way to add a folder to the JRuby classpath with Rails?
Require java first. That's what makes the $CLASSPATH variable live.
include Java
$CLASSPATH << "your/folder"
In pre-1.0 versions of JRuby, you'd do require 'java' instead, but in modern JRuby that silently doesn't work.
Related
Is there a environment variable and some command-line options to check my rails application's directories that are set automatically or configured somewhere so that rails know where to load libraries etc.?
As a compare, ruby has a command line option (-I):
ruby -I path:another/path:/usr/lib rubyfile.rb arg1 arg2
such that users can set the directories where libs can be looked up by Ruby.
Similarly, here I am asking if for rails there is a similar way to check the loading paths for Rails applications? Or if there is certain variable set somewhere?
Highlight: I am using Rails version 4.2.0. The config/application.rb sample code doesn't appear to contain config.autoload_paths, not even in comments, which used to be the one for Rails 3 and Rails 4.1.x.
And, how would the Rails application know where to look up for Gems, etc.?
Thanks!
See this file:
config/application.rb
Specifically, you add this setting:
config.autoload_paths
You can add to the autoload paths like this:
config.autoload_paths << Rails.root.join('mydir')
The config.autoload_paths accepts an array of paths from which Rails will autoload constants.
The default is all directories under ./app, such as controllers, models, etc.
The default does not autoload from the typical top-level ./lib directory. If you want to autoload from lib:
config.autoload_paths << Rails.root.join('lib')
See the Rails Guides for Configuring
Rails knows where to look for gems a variety of ways: typically your environment GEM_PATH or the bundler gem bundle exec command. For example, I personally bundle my Rails gems into ./vendor/bundle/.
I have been using the surveyor gem within Rails 3.2.x without any issues in my project.
The gem defines modules that reside within the lib subdirectory of the gem.
Example
lib/surveyor/helpers/surveyor_helper_methods.rb
Then in my app/helpers directory I include the module and extend like follows.
include Surveyor::Helpers::SurveyorHelperMethods
This works fine in Rails 3, but within Rails 4 it results in the error Uninitialized constant Surveyor::Helpers.
As a test I copied the directory from the gem directly into my projects lib directory structure and this got rid of the error; so it seems the include is no longer looking at the gems' lib tree. Moving all of the files directly up into my project isn't a good solution. Is there another way to work around this?
in your helper , just include this file..so it will be something like
require 'surveyor/helpers/surveyor_helper_methods'
module UserHelper
include Surveyor::Helpers::SurveyorHelperMethods
end
I'm creating a plugin for my Rails 2.3.8 and inside my plugin's init.rb file I have the following code
"#{RAILS_ROOT}/log/myerror.log"
I'm trying to create a 'myerror.log' file in log/ folder), but it seems like plugin can't read the RAILS_ROOT variable (I'm getting the following error in my plugins unit tests:
`const_missing': uninitialized constant RAILS_ROOT (NameError)
I did some web-searching but couldn't find an answer. How to run unit tests with in a plugin with 'RAILS_ROOT') variable, or what are the other best practices?
I'm running on Rails 2.3.8 on Linux.
You should define the RAILS_ROOT constant by using
RAILS_ROOT = File.join(File.expand_path(File.dirname(__FILE__)), '../../../')
In my project, I have written few classes under lib folder but rails is not detecting those classes in production environment. I get the uninitalized Constant error.
I use Apache in the production environment and rails script/server in the development environment.
Is anything wrong with RAILS_ROOT environment? Can anyone suggest how to overcome this problem?
I am not sure about Rails, but you achieve that in Ruby by this: (it will work in rails too, but rails must be having some elegant way)
require File.join(File.dirname(__FILE__), "lib",'your_module_name')
include your_module_name
Try this in config/application.rb (I assume you have rails3)
config.load_paths += %W( #{config.root}/lib )
Update: Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?
Ensure that the name of your file matches the name of the class or module defined in it, accounting for any directories.
ie:
lib/my_new_class.rb
class MyNewClass
end
Or if you have a directory hierarchy:
lib/my_files/my_module.rb
module MyFiles
module MyModule
end
end
I've been fighting left and right with rails 3 and bundler. There are a few gems out there that don't work properly if the rails application hasn't been loaded yet. factory_girl and shoulda are both examples, even on the rails3 branch.
Taking shoulda as an example, when trying to run rake test:units I get the following error:
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. (called from autoload_macros at c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40)
c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'join': can't convert #<Class:0x232b7c0> into String (TypeError)
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'block in autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'map'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/rails.rb:17:in '<top (required)>'
Digging a bit deeper into lib/shoulda/rails, I see this:
root = if defined?(Rails.root) && Rails.root
Rails.root
else
RAILS_ROOT
end
# load in the 3rd party macros from vendorized plugins and gems
Shoulda.autoload_macros root, File.join("vendor", "{plugins,gems}", "*")
So...what's happening here is while Rails.root is defined, Rails.root == nil, so RAILS_ROOT is used, and RAILS_ROOT==nil, which is then being passed on to Shoulda.autoload_macros. Obviously the rails app has yet to be initialized. With Rails3 using Bundler now, there's been some hubub over on the Bundler side about being able to specify an order in which the gems are required, but I'm not sure whether or not this would solve the problem at hand.
Ultimately my questions is this: When exactly does the environment.rb file (which actually initializes the application) get pulled in? Is there any harm to bumping up when the app is initialized and have it happen before the Bundler.require line in config/application.rb? I've tried to hack bundler to specify the order myself, and have the rails gem pulled in first, but it doesn't appear to me that requiring the rails gem actually initializes the application.
As this line (in config/application.rb) is being called before the app is initialized, any gem in the bundler Gemfile that requires rails to be initialized is going to tank.
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env
Well, it was actually fairly easy to trace this down. All the rails libraries are being pulled in in application.rb. The app itself is being initialized in environment.rb.