rails 4.0.0, will_paginate 3.0.5, not showing second page #357

Closed
klossaki opened this Issue Feb 3, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@klossaki

klossaki commented Feb 3, 2014

Hello everyone,
I have created a custom search of books according to the criteria on the title of the book the user enters and I use will_paginate to show them. However, even though the first page shows ok, when I choose the second the following error message appears:

"Couldn't find Book with id=show_search_by_title` in show method", which is the following:

def show
  @book = Book.find(params[:id])
  authorize! :read, @book
end 

In the books_controller there is the following method:

def show_search_by_title
  q = params[:book][:title]
  books = Book.where('title LIKE ?', "%#{q}%")
  @books = books.paginate(page: params[:page], per_page: 10)
end

and the corresponding .erb file is the following:

<% if @books.empty? %>
  There are no books in the Library that fulfill the given criteria
<% else %>
  <%= will_paginate @books %>
<h2>The books that complete the given criteria are the following:</h2>
<table>    
<tr>
  <th> Book Title </th> <th> Author </th> <th> Category </th> <th> copies </th> <th> Rank </th>
</tr>

<% @books.each do |book| %>
<tr>
  <td><%=book.title %> </td>
  <td><%=book.author.name %> </td>
  <td><%=book.cat.description %></td>
  <td><%=book.copies %>  </td>
  <td><%=book.rank %> </td>
</tr>
<% end %>
</table>
<%= will_paginate @books %>
<% end %>

my routes for books are:

view_books_books_path   GET     /books/view_books(.:format)     books#view_books

rank_book_books_path    GET     /books/rank_book(.:format)  books#rank_book

update_book_rank_books_path     PATCH   /books/update_book_rank(.:format)   

books#update_book_rank

search_by_title_books_path  GET     /books/search_by_title(.:format)    

books#search_by_title

search_by_author_books_path     GET     /books/search_by_author(.:format)   

books#search_by_author

show_search_by_title_books_path     GET     /books/show_search_by_title(.:format)   

books#show_search_by_title

show_search_by_author_books_path    POST    /books/show_search_by_author(.:format)  

books#show_search_by_author

books_path  GET     /books(.:format)    books#index

    POST    /books(.:format)    books#create

new_book_path   GET     /books/new(.:format)    books#new

edit_book_path  GET     /books/:id/edit(.:format)   books#edit

book_path   GET     /books/:id(.:format)    books#show 

shows the error in show method:

def show
@book = Book.find(params[:id])
authorize! :read, @book
end 

Thank you very much in advance

@mislav

This comment has been minimized.

Show comment Hide comment
@mislav

mislav Feb 3, 2014

Owner

I tried to reproduce your issue with Rails 4.0.0, will_paginate v3.0.5 and these routes:

  resources :books do
    collection do
      get 'show_search_by_title'
    end
  end
$ rake routes | grep books
show_search_by_title_books GET    /books/show_search_by_title(.:format) books#show_search_by_title
                     books GET    /books(.:format)                      books#index
                           POST   /books(.:format)                      books#create
                  new_book GET    /books/new(.:format)                  books#new
                 edit_book GET    /books/:id/edit(.:format)             books#edit
                      book GET    /books/:id(.:format)                  books#show
                           PATCH  /books/:id(.:format)                  books#update
                           PUT    /books/:id(.:format)                  books#update
                           DELETE /books/:id(.:format)                  books#destroy

However, everything works as expected. I suspect the problem is somewhere in your routing setup, as I believe you made it overly complex. Or maybe it's another plugin breaking something. Without access to the app, I think the only person who could help you now is you.

Owner

mislav commented Feb 3, 2014

I tried to reproduce your issue with Rails 4.0.0, will_paginate v3.0.5 and these routes:

  resources :books do
    collection do
      get 'show_search_by_title'
    end
  end
$ rake routes | grep books
show_search_by_title_books GET    /books/show_search_by_title(.:format) books#show_search_by_title
                     books GET    /books(.:format)                      books#index
                           POST   /books(.:format)                      books#create
                  new_book GET    /books/new(.:format)                  books#new
                 edit_book GET    /books/:id/edit(.:format)             books#edit
                      book GET    /books/:id(.:format)                  books#show
                           PATCH  /books/:id(.:format)                  books#update
                           PUT    /books/:id(.:format)                  books#update
                           DELETE /books/:id(.:format)                  books#destroy

However, everything works as expected. I suspect the problem is somewhere in your routing setup, as I believe you made it overly complex. Or maybe it's another plugin breaking something. Without access to the app, I think the only person who could help you now is you.

@mislav mislav closed this Feb 3, 2014

@klossaki

This comment has been minimized.

Show comment Hide comment
@klossaki

klossaki Feb 3, 2014

thank you very much for replying. I solved the problem, I changed my route
from post to get and everything turned up fine.

I tried to reproduce your issue with Rails 4.0.0, will_paginate v3.0.5 and
these routes:

  resources :books do
    collection do
      get 'show_search_by_title'
    end
  end
$ rake routes | grep books
show_search_by_title_books GET    /books/show_search_by_title(.:format)
books#show_search_by_title
                     books GET    /books(.:format)
books#index
                           POST   /books(.:format)
books#create
                  new_book GET    /books/new(.:format)
books#new
                 edit_book GET    /books/:id/edit(.:format)
books#edit
                      book GET    /books/:id(.:format)
books#show
                           PATCH  /books/:id(.:format)
books#update
                           PUT    /books/:id(.:format)
books#update
                           DELETE /books/:id(.:format)
books#destroy

However, everything works as expected. I suspect the problem is somewhere
in your routing setup, as I believe you made it overly complex. Or maybe
it's another plugin breaking something. Without access to the app, I think
the only person who could help you now is you.


Reply to this email directly or view it on GitHub:
#357 (comment)

klossaki commented Feb 3, 2014

thank you very much for replying. I solved the problem, I changed my route
from post to get and everything turned up fine.

I tried to reproduce your issue with Rails 4.0.0, will_paginate v3.0.5 and
these routes:

  resources :books do
    collection do
      get 'show_search_by_title'
    end
  end
$ rake routes | grep books
show_search_by_title_books GET    /books/show_search_by_title(.:format)
books#show_search_by_title
                     books GET    /books(.:format)
books#index
                           POST   /books(.:format)
books#create
                  new_book GET    /books/new(.:format)
books#new
                 edit_book GET    /books/:id/edit(.:format)
books#edit
                      book GET    /books/:id(.:format)
books#show
                           PATCH  /books/:id(.:format)
books#update
                           PUT    /books/:id(.:format)
books#update
                           DELETE /books/:id(.:format)
books#destroy

However, everything works as expected. I suspect the problem is somewhere
in your routing setup, as I believe you made it overly complex. Or maybe
it's another plugin breaking something. Without access to the app, I think
the only person who could help you now is you.


Reply to this email directly or view it on GitHub:
#357 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment