At the moment I'm having to restart the server if I change files in the lib/ and app/helpers/ directories. Is there a way to configure either Rails or Webrick such that it hotswaps the files in those directories as well?
In your config/application.rb:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/helpers)
There should be a commented line which explains what autoload_paths is for.
Related
I want to include some additional non-rails classes in my Rails App. I created a lib directory under app/ and a lib directory under test/ I am using minitest.
How do I make it so rails test also runs the tests under the new test/lib directory? How do I make it so my app includes app/lib in the autoload mechanism?
In the application.rb file add the following configuration which will load files from the app/lib folder
config.autoload_paths += "#{Rails.root}/app/lib/"
I recently upgraded my rails app from Thin to Puma, hosted on Heroku. Everything works perfectly on staging environment, but when I deploy to production no modules or classes seem to be loaded.
The app launches and run on production but whenever one of the classes in my /lib directory is needed I get a NameError (uninitialized constant) error.
In my application.rb file this is where I load the lib files:
config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]
Running Rails 3.1.1 and Ruby 2.1.1
I can't work out why they would load in staging but not production. Any help would be much appreciated!
Try Rails.root instead of config.root.
You should try add those line to application.rb:
config.eager_load_paths << Rails.root.join('lib')
config.autoload_paths << Rails.root.join('lib')
It worked for me
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 was wondering if there is a way to easily access methods/classes from the lib folder in migrations? I found a way to require them with
require File.expand_path("../../lib/my_library")
I don't really like this and I was wondering if there is a more common way to include those libraries required by the migration?
rake db:migrate load all rails environment, so you can just add lib in autoload in application.rb file like this and it should work.
config.autoload_paths += Dir["#{config.root}/lib/**/"]
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