与rails中的模块和型号名称混淆

时间:2017-03-23 11:19:16

标签: ruby-on-rails ruby namespaces

我在项目中为Customer实现了完整的CRUD系统。 customers_controller.rb的路径是

app/controllers/admin/customers_controller.rb

除此之外,我必须添加新功能,所以我创建了一个新的控制器feature_controller.rb(名称已更改),文件路径如下

app/controllers/admin/customer/features_controller.rb

控制器内的代码

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
....

然后我删除了新控制器和文件夹,但仍然是同样的错误。

我在Customer中查看了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}

1 个答案:

答案 0 :(得分:2)

使用常数分辨率运算符(x <- read.csv(file.choose(), header="TRUE", sep=',') )。

::

会为你解决,因为这样你就告诉rails在当前范围之外寻找@customer = ::Customer.new customer_params 常量。