0

So I have a search form that returns a Kaminari paginated array. The first page always returns a list of results, however the "GET" of all subsequent page links returns no results and I'm not sure why!

Here are my search methods in my controller

def writer_search
  @writers = Kaminari.paginate_array(@results).page(params[:page]).per(10)
end

def writer_search_submit
  @results = #my big array of results, this part works fine
  @writers = Kaminari.paginate_array(@results).page(params[:page]).per(10)
  render 'writer_search'
end

View Code

 =form_tag(writer_search_submit_path, :method => 'post') do
   %input{:name => 'keywords', :id => 'keywords', :value => params[:keywords]}
   = submit_tag "Search"

 - @writers.each do |writer|
   #show the results

 = paginate @writers

Server Log when clicking a pagination link

Started GET "/editors/writer_search?commit=Search&keywords=business&page=2"
  Processing by EditorsController#writer_search as HTML
  Parameters: {"commit"=>"Search", "keywords"=>"business", "page"=>"2", "utf8"=>"✓"}

The first rendered page has the first 10 results of my array (in this case, there are hundreds of results). Clicking on any of the pagination links makes a GET and returns a page with no results.

Any ideas?

thoughtpunch
  • 1,907
  • 4
  • 25
  • 41

2 Answers2

3

Looks like you have @results defined in writer_search_submit but not writer_search.

The GET request to "/editors/writer_search?commit=Search&keywords=business&page=2" goes through EditorsController#writer_search where @results is undefined (and so you get a page with no results).

Update: (Thanks Thilo)

Perhaps you could use a before_filter to load @results for all the actions that need it?

DanS
  • 17,550
  • 9
  • 53
  • 47
  • 1
    This seems correct, but don't store data in the session unless you absolutely have to. Use a `before_filter` to load @results for all the actions that need it. – Thilo Apr 11 '12 at 17:29
0

Rails doesn't render the search results on an actually search page but on the default index page of whatever route you're searching for.

CrazyCoderMonkey
  • 423
  • 1
  • 4
  • 12