時候我已經應用程序的文件夾結構下創建一個新的類在我的Rails應用程序/數據表Rails的實例
類保存在這個目錄中的文件,並將其保存爲DatasetIndexDatatable.rb
未初始化常數類文件的內容如下:
class DatasetIndexDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
end
當我試圖從我的應用的控制器實例化這個類,Rails的給出了一個錯誤:
uninitialized constant DatadescriptionController::DatasetIndexDatatable
它試圖實例化新類控制器中的代碼如下:
class DatadescriptionController < ApplicationController
layout "datadescription"
def index
respond_to do |format|
format.html
format.json { render json: DatasetIndexDatatable.new(view_context) }
end
end
end
爲什麼不能Rails的看到新類?我曾嘗試添加含有類到config.autoload_paths變量在application.rb中的文件夾中:
config.autoload_paths += %W(#{config.root}/lib
#{config.root}/datatables)
但是出現同樣的錯誤。我也曾嘗試使用全局命名空間中實例化新類控制器:
format.json { render json: ::DatasetIndexDatatable.new(view_context) }
,並使用包含文件夾的類爲命名空間:
format.json { render json: Datatables::DatasetIndexDatatable.new(view_context) }
都無濟於事。我究竟做錯了什麼?
你的問題就成了我的答案:d – SSR