我无法弄清楚这个非常简单的错误:
xyz_controller.rb:
@isbn = params[:q]
@search_type = params[:search_type]
...
@prices = Generalsearch.new(:search_term => @isbn, :search_type => @search_type)
generalsearch.rb
attr_accessor :search_term , :search_type
def initialize(search_term , search_type)
self.search_term= search_term
self.search_type= search_type
end
...
我一直在
wrong number of arguments (1 for 2)
app/models/generalsearch.rb:11:in `initialize'
app/controllers/book_controller.rb:47:in `new'
app/controllers/book_controller.rb:47:in `view'
答案 0 :(得分:7)
你只传递一个参数,即在Generalsearch.new()
中散列:search_term => @isbn, :search_type => @search_type
使用
Generalsearch.new( @isbn, @search_type)
答案 1 :(得分:4)
你必须使用,因为你在初始化函数上接受2个参数,而不是参数的散列。
@prices = Generalsearch.new(@isbn, @search_type)
答案 2 :(得分:0)
如果你想使用
Generalsearch.new(:search_term => @isbn, :search_type => @search_type)
然后你可以使用initialize
方法
def initialize(options)
# You can also use options[:search_term],
# but fetch lets you know if the key doesn't exist
self.search_term= options.fetch(:search_term)
self.search_type= options.fetch(:search_type)
end