0

If I do:

rails generate scaffold account/user username

I get a controller that looks like this:

class Account::UsersController < ApplicationController
  def index
    @account_users = Account::User.all
  end
...
end

If I include the Account Module, then it looks like all the database calls don't need to be prefixed with "Account::". I.e.

class Account::UsersController < ApplicationController

  include Account

  def index
    @account_users = User.all #this works because I included the Account Module above
  end
...
end

Now if I were to move my

controllers/account/users_controller.rb

file to:

controllers/admin/account/users_controller.rb

The file looks like this (note: I also corrected my routes file after this move):

class Admin::Account::UsersController < ApplicationController

  include Account

  def index
    @account_users = User.all #this call does not work now
  end
...
end

But I get an error saying "uninitialized constant Admin::Account::UsersController::User"

It looks like rails is trying to make a database call on the "User" model without the "Account::" module in front of it.

So how does including modules in controllers work? Why does this not work when I move my controller into a different file (and leave the model in the same location from the generated scaffold) but it works with the scaffold generated files? How can I fix this issue?

4

2 回答 2

0

我想我没有意识到你可以明确地要求你想要包含的模块的路径。在阅读了更多模块后,我了解到了这一点...

因此,在控制器类之外添加对“需要'帐户/用户'”的显式调用使得在控制器中包含模块可以正常工作。

于 2013-08-11T04:25:17.007 回答
0

解析模块名称是相对于当前模块完成的。尝试将其更改为:

include ::Account

或者

include ::Admin::Account

(取决于定义用户模型的模块)

这将告诉 ruby​​ 在全局命名空间中查找模块Account

于 2013-08-04T19:25:44.387 回答