Reputation: 10667
I have a project on Rails 5.2 with the following structure:
oauth_controller.rb
require_relative "./oauth.rb"
module Auth
class OauthController < Infra::BaseController
include ActionController::Cookies
def start
oauth = Auth::OAuth.new(session: session)
...
end
def callback
oauth = Auth::OAuth.new(session: session)
...
end
end
end
oauth.rb
module Auth
class OAuth
...
end
end
To have the Auth::Oauth
working I have to require the oauth.rb
file, so I think the eager loading or autoload are not working. But, even with the require()
, when I change the file, I get this error again and I have to restart the server again and again.
uninitialized constant Auth::OAuth
Here is my application.rb
config.middleware.use ActionDispatch::Cookies
config.api_only = false
config.eager_load_paths += %W(#{config.root}/app)
config.time_zone = 'Etc/UTC'
config.reload_controllers = !Rails.env.production?
The development.rb was not changed.
Upvotes: 2
Views: 254
Reputation: 2384
It's because of naming convention rails expects. Rails is expecting a file name o_auth.rb
to match OAuth
. You need to add an infection to support OAuth
as oauth.rb
In config/initializers/inflections.rb
add
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'OAuth'
end
OR you need to change your file name to o_auth.rb
In both cases you do not need require_relative "./oauth.rb"
In addition, if this is a controller it should live in app/controllers/auth/o_auth
and not directly in app/
Upvotes: 4
Reputation: 20263
In addition to the other answer, the folders under app
are not interpreted as modules and are for organization only. So app/auth/oauth.rb
Should look like
class Oauth
Not
module Auth
class Oauth
Upvotes: 0