开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜