Jump to content

The ultimate community for Ruby on Rails developers.


Photo

ArgumentError - wrong number of arguments (1 for 2)

Ruby On Rails Simple search

  • Please log in to reply
6 replies to this topic

#1 Michel

Michel

    Passenger

  • Members
  • 3 posts

Posted 16 December 2013 - 05:51 PM

Hi,

 

I am trying to make a simple search on my project but I am struggling a little. I keep getting this error:

ArgumentError in ProductsController#search

wrong number of arguments (1 for 2)

 

Application Trace | Framework Trace | Full Trace
app/models/product.rb:11:in `search'
app/controllers/products_controller.rb:20:in `search'

 

 

 

I Have that in my model:

 
def self.search(search, page)
conditions = nil
unless search.blank?
conditions = ['name like ?', "%#{search}%"]
end
paginate :per_page => 3, :page => page, :conditions => conditions, :order => 'name'
end

 and this in my controller

def index
#@products = Product.paginate(:page => params[:page], :per_page => 3)
#@products = Product.paginate(params[:search], params[:page])
@products = Product.search(params[:search], params[:page])


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end
  
  def search
@products = Product.search params[:q]
#@products = Product.paginate(:page => params[:page], :per_page => 3)
#@products = Product.search(params[:q], :page =>  params[:page], :per_page =>3)


render 'index'                 
end

I am not sure how to do the paginate so it shows the product I am searching for. I would be very thankful if someone can help me. Thank you.

 



#2 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 16 December 2013 - 09:31 PM

In your model you are defining the method search as a class method. It takes two arguments. In the search method in the controller you are calling this, but only with one argument, which is why you get the error you do.


Blog: http://ohm.sh | Twitter: madsohm


#3 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 16 December 2013 - 09:47 PM

Ohm is right.

 

I wouldn't put the paginate method inside of the model either. I'd leave that to the controller unless you wanted to set a default per page integer you can do that but this logic is better served in the controller.


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#4 Michel

Michel

    Passenger

  • Members
  • 3 posts

Posted 17 December 2013 - 12:34 AM

I am sorry I know it is very simple but I am only learning. Do you have any suggestions how to get my  search to work ? I did put that in my search method so it doesn't give me the error anymore but it doesn't do anything.  Thanks in advance.

 @products = Product.search(params[:search], params[:page]) 


#5 Michel

Michel

    Passenger

  • Members
  • 3 posts

Posted 17 December 2013 - 12:43 PM

Well I got this to work actually but it does not give me any result when I search.  :(



#6 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 17 December 2013 - 09:45 PM

Well you're just defining a conditions... You're not actually running a query from the looks of things.


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#7 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 17 December 2013 - 10:39 PM

So, as Jamie is saying, you might not actually be using your conditions.

 

The correct - Rails - way would be to do this with a scope on Product

scope :search, ->(query) { where('name like ?', "%#{query}%" }

and then using this from the controller like so

def index
  @products = Product.search(params[:search]).paginate(page: params[:page], per_page: 3)
  respond_to do |format|
    format.html
    format.json { render json: @products }
  end
end

def search
  @products = Product.search(params[:q])
  render 'index'
end
  

  • james likes this

Blog: http://ohm.sh | Twitter: madsohm





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users