Free-Minded
Free-Minded

Reputation: 5430

will_paginate with search function is not working in ruby on rails

I am trying to search my posts item with pagination. I am doing like this..

@posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)

But it showing NoMethodError

undefined method `paginate' for #<Array:0x9a93f08>

I don't know where i am wrong. Please help

Upvotes: 1

Views: 1666

Answers (3)

Free-Minded
Free-Minded

Reputation: 5430

I just change my code

@posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)

to this

@posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search])

Now it works fine :)

Upvotes: 3

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22296

I will advise you to use Kaminari instead. As you can see by the Readme there's one section on to paginate Arrays. Also make sure you read the Kaminari recipes where is an example on paginate Arrays.

Upvotes: 0

Arup Rakshit
Arup Rakshit

Reputation: 118271

params[:search]) gives you an array. And Array don't have paginate method, so errors thrown up. Use p params[:search]).inspect to see the same.

Upvotes: 0

Related Questions