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.

Kevin Brown
  • 12,602
  • 34
  • 95
  • 155
  • 3
    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. – Lasse Bunk Jul 14 '16 at 17:20

3 Answers3

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.

Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67
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.

Alvaro Inckot
  • 695
  • 9
  • 29
  • 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? – Kevin Brown Apr 10 '16 at 18:36
  • 1
    Sure! You can read more about rails plugins [here](http://guides.rubyonrails.org/plugins.html). 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. – Alvaro Inckot Apr 11 '16 at 18:57
  • You also can require the lib folder using: `config.autoload_paths << Rails.root.join('lib')` – Alvaro Inckot Apr 27 '16 at 18:50
  • 2
    @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? – Daniel Dec 06 '16 at 11:10
  • 1
    @Daniel This answer can help you: http://stackoverflow.com/a/19852844/3182764. Basically, autoload will require your class when it will be called. – Alvaro Inckot Dec 08 '16 at 12:22
  • 1
    @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! – Daniel Dec 10 '16 at 12:37
-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

Roman Usherenko
  • 4,659
  • 4
  • 17
  • 14
  • 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. – Kevin Brown Apr 11 '16 at 23:35