wrong number of arguments - in initialize method
I am not able to figure out this extremely simple error:
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
...
开发者_运维技巧I keep getting
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'
you are passing only one argument ie hash :search_term => @isbn, :search_type => @search_type
in Generalsearch.new()
use
Generalsearch.new( @isbn, @search_type)
You have to use, as you're accepting 2 params on the initialize function, not a hash of of params.
@prices = Generalsearch.new(@isbn, @search_type)
If you want to use
Generalsearch.new(:search_term => @isbn, :search_type => @search_type)
Then you can have in the initialize
method
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
精彩评论