1

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' 
0

3 Answers 3

7

you are passing only one argument ie hash :search_term => @isbn, :search_type => @search_type in Generalsearch.new()

use

Generalsearch.new( @isbn, @search_type)
Sign up to request clarification or add additional context in comments.

Comments

4

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.