V
V
Vasily Shakhunov2018-05-18 09:29:16
Ruby on Rails
Vasily Shakhunov, 2018-05-18 09:29:16

What can be done with /lib autoloading?

Some strange behavior of rails.
There is a route

namespace :api do
    namespace :v1 do
      get 'roistat/calls', to: 'roistat#calls'
    end
  end

There is a controller
class Api::V1::RoistatController < ActionController::Base

  def calls
    render :ok
  end
end

We kick it, and we see:
Started GET "/api/v1/roistat/calls" for 127.0.0.1 at 2018-05-18 09:19:54 +0300

LoadError (Unable to autoload constant Api, expected /path/to/project/lib/roistat/api.rb to define it):
  activesupport (4.2.7.1) lib/active_support/dependencies.rb:495:in `load_missing_constant'
  activesupport (4.2.7.1) lib/active_support/dependencies.rb:184:in `const_missing'

It would seem... what does /lib have to do with it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vasily Shakhunov, 2018-05-19
@inf

Some bug with autoloading modules from /lib (like here https://github.com/thoughtbot/factory_bot/issues/670)
The easiest way was to rename /lib/roistat/api.rb to /lib/roistat/client. rb

V
Vladimir Miroshnichenko, 2018-05-18
@gururuby

Check if you have the same module and class names, it's possible that rails doesn't resolve dependencies correctly.
And I'd rather structure

module Api
  module V1
     class RoistatController < ActionController::Base
     end
  end
end

V
Vayladion Gognazdiak, 2018-05-19
@etspring

I'm sorry, but wouldn't it be better to do:

scope :api do
    namespace :v1 do
      get 'roistat/calls', to: 'roistat#calls'
    end
end

PS: and finally it seems to me that these are the machinations of beeches;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question