我为 Customer
实现了完整的 CRUD 系统在项目中。 customers_controller.rb
的路径是
app/controllers/admin/customers_controller.rb
最重要的是,我必须添加新功能,所以我创建了一个新 Controller
feature_controller.rb
(名称已更改)具有这样的文件路径app/controllers/admin/customer/features_controller.rb
Controller 内的代码
module Admin
module Customer
class FeatureController < ApplicationController
def index; end
end
end
end
所以它对我的旧功能有一些影响,因为当我通常创建客户时,它报告了这个错误:
NoMethodError (undefined method `new' for Admin::Customer:Module):
app/controllers/admin/customers_controller.rb:21:in `create'
第 21 行是:
@customer = Customer.new customer_params
if @customer.save
....
然后我删除了 新 Controller 和文件夹 但仍然是同样的错误。
我看了
Customer
的类型类(class)binding.pry
并且说它是Module
.更新
development.rb
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/*"].select{|f| File.directory? f}
最佳答案
使用常量分辨率运算符 ( ::
)。
@customer = ::Customer.new customer_params
会为你解决它,因为这样你会告诉 rails 寻找
Customer
当前范围之外的常量。
关于ruby-on-rails - 与 rails 中的模块和型号名称混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974546/