Rails autoload / persistent resolution creates ghost modules

Here's a new Rails 5.1.4 application with a model and multiple routes and controllers.

A controller with a name extension refers to a top-level model:

class AdminArea::WelcomeController < ApplicationController
  def index
    @user = User.new(name: 'Sergio')
  end
end

So far so good. You can check the wizard, go to http://localhost:3000/admin_area/welcomeand see how it works.

BUT, if we were to add an empty directory app/presenters/admin_area/user/* , then everything becomes strange. Suddenly, Userin this controller, not my model, but a nonexistent module!

NoMethodError (undefined method `new' for AdminArea::User:Module):

app/controllers/admin_area/welcome_controller.rb:3:in `index'

Naturally, this module does not have any [non-built-in] methods and cannot be attached to the source file on disk.

: , User ?


* , , .

NameError ( AdminArea:: WelcomeController:: User)

git , .keep. , .

+6
1

Ruby , Rails .

User " " , , . , :

AdminArea::WelcomeController::User
AdminArea::User
User

Rails autoload_path, , . :.

app/assets/admin_area/welcome_controller/user.rb
app/assets/admin_area/welcome_controller/user
app/channels/admin_area/welcome_controller/user.rb
...
app/assets/admin_area/user.rb
app/assets/admin_area/user
...
app/assets/user.rb
...
app/models/user.rb #=> here it is!

admin_area/user , ​​. Rails , , , .

, Rails:

...
app/assets/admin_area/user.rb
app/assets/admin_area/user
...
app/presenters/admin_area/user #=> Here Rails finds the folder

Rails User .

. User, AdminArea ( AdminArea::User), " " , ::.

@user = ::User.new(name: 'Sergio')
+5

All Articles