Rails 5 ignoring /lib class?
Asked Answered
S

3

27

I've used this method for modals in rails. It works really well, but I've just upgraded to Rails 5 beta3, and now it's not working in production.

I get this error:

Completed 500 Internal Server Error in 22ms (ActiveRecord: 0.9ms)

NameError (uninitialized constant ApplicationController::ModalResponder):

app/controllers/application_controller.rb:26:in `respond_modal_with'
app/controllers/tools_controller.rb:28:in `new'

Is my inheritance thrown off with Rails 5?

My class ModalResponder < ActionController::Responder is in /lib and works in development...

Looking for info on changes with rails 5, but sources are limited to my knowledge.

Stigmatize answered 5/4, 2016 at 18:43 Comment(1)
This seems like a bug in Rails 5.0.0. It is also present after being out of beta. For me, it only happens in production, not in development.Finely
I
12

You need to add a 'require' (on application.rb) with your classes inside the lib folder.

Like:

require './lib/someclass'

I recommend you put it inside a Rails plugin.

Interception answered 5/4, 2016 at 20:24 Comment(6)
can you describe the best process for making it a plugin? I moved the lib file to my /app directory so it would be included, but that's probably not best?Stigmatize
Sure! You can read more about rails plugins here. It's better to test all and you can add load file settings inside the plugin. Just create a plugin and move you lib folder inside them, I recommend to create a file 'Foo' to add the requires. In your application.rb project just add require 'foo'. Sorry my english hahah.Interception
You also can require the lib folder using: config.autoload_paths << Rails.root.join('lib')Interception
@Arvro adding lib to the autoload_paths alone doesn't seem to do the trick. You have to explicitly require the files you want to autoload in production. At least that's what I had to do to get it to work. Anyone knows if this is intended behaviour and why?Agonistic
@Agonistic This answer can help you: https://mcmap.net/q/293321/-confusing-about-autoload_paths-vs-eager_load_paths-in-rails-4. Basically, autoload will require your class when it will be called.Interception
@Arvro perfect. Thank you! TL;DR for everyone else: add the lib path to config.autoload_paths and config.eager_load_paths to get your code loaded and required in production!Agonistic
F
42

In config/application.rb, change this:

config.autoload_paths << Rails.root.join('lib')

to this:

config.eager_load_paths << Rails.root.join('lib')

eager_load_paths will get eagerly loaded in production and on-demand in development. Doing it this way, you don't need to require every file explicitly.

See more info on this answer.

Fellows answered 4/5, 2017 at 17:22 Comment(0)
I
12

You need to add a 'require' (on application.rb) with your classes inside the lib folder.

Like:

require './lib/someclass'

I recommend you put it inside a Rails plugin.

Interception answered 5/4, 2016 at 20:24 Comment(6)
can you describe the best process for making it a plugin? I moved the lib file to my /app directory so it would be included, but that's probably not best?Stigmatize
Sure! You can read more about rails plugins here. It's better to test all and you can add load file settings inside the plugin. Just create a plugin and move you lib folder inside them, I recommend to create a file 'Foo' to add the requires. In your application.rb project just add require 'foo'. Sorry my english hahah.Interception
You also can require the lib folder using: config.autoload_paths << Rails.root.join('lib')Interception
@Arvro adding lib to the autoload_paths alone doesn't seem to do the trick. You have to explicitly require the files you want to autoload in production. At least that's what I had to do to get it to work. Anyone knows if this is intended behaviour and why?Agonistic
@Agonistic This answer can help you: https://mcmap.net/q/293321/-confusing-about-autoload_paths-vs-eager_load_paths-in-rails-4. Basically, autoload will require your class when it will be called.Interception
@Arvro perfect. Thank you! TL;DR for everyone else: add the lib path to config.autoload_paths and config.eager_load_paths to get your code loaded and required in production!Agonistic
I
-2

It says it cannot find the ApplicationController::Responder which was moved out of Rails 4.2 into a separate gem.

Add gem 'responders' to your Gemfile

Classes in lib are not autoloaded, you have to require them

Incarnate answered 5/4, 2016 at 19:11 Comment(1)
I can select this as an answer if you can provide reasoning for why lib is not autoloaded. This answer is not detailed enough to merit a check.Stigmatize

© 2022 - 2024 — McMap. All rights reserved.