rails 4 will_paginate自定义记录第二页未显示

时间:2014-02-03 13:45:00

标签: ruby-on-rails will-paginate

我根据用户输入的条件创建了自定义书籍搜索,并使用will_paginate来显示它们。但是,即使第一页显示正常,当我选择第二页时,会出现以下错误消息:

Couldn't find Book with id=show_search_by_title` in show method
def show
  @book = Book.find(params[:id])
  authorize! :read, @book
end 

在books_controller中有以下方法:

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

和相应的.erb文件如下:

<% if @books.empty? %>
  There are no books in the Library that fulfill the given criteria
<% else %>
  <%= will_paginate @books %>

完成给定标准的书籍如下:

<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 %>

非常感谢你。

我的书籍路线是:

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 

显示show方法中的错误:

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

1 个答案:

答案 0 :(得分:1)

您需要在will_paginate https://github.com/mislav/will_paginate/wiki/API-documentation#will_paginatecollection-options的选项中指定操作。 “下一步”按钮的链接路径是什么?