您好我在导轨3个文件如下: 1)位于 “应用程序/控制器/ listings_controller.rb”滑轨: “NoMethodError:为#未定义的方法`constructKDTree” <类别:0x00000104b1f760>”
class ListingsController < ApplicationController
def index
#Construct kd Tree in memory
@tree = Listing.constructKDTree;
@tree.inspect
end
2)位于应用程序/模型/ listing.rb
require 'kd_tree.rb'
class Listing < ActiveRecord::Base
def constructKDTree
@contents = self.all
@kdTree = KDTree.new(@contents)
end
3)位于应用程序/模型/ kd_tree.rb
class KDTree
def initialize (db_listings)
'Initializing Tree'
end
end
现在我想测试constructKDTree
方法实现,所以我去了我的铁轨控制台并尝试下面的命令:
1.9.2-p290 :001 > @lc = ListingsController.new
=> #<ListingsController:0x00000104f3e288 @_routes=nil, @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil>
1.9.2-p290 :002 > @lc.index
但我得到这个错误:
NoMethodError: undefined method `constructKDTree' for #<Class:0x00000104b1f760>
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from /Users/AM/Documents/RailsWS/cmdLineWS/Businesses/app/controllers/listings_controller.rb:20:in `index'
from (irb):2
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我在做什么错误?
您好感谢。我来自Java背景并学习Ruby。所以类级方法就像Java中的静态方法? – banditKing 2012-03-24 22:06:31
@banditKing:是的,这是最接近你可以得到的。 – 2012-03-24 22:07:56