0

How do I use pagination on the same partial with different variables passed?

I have a library show page in my Rails app that has a list of books:

libraries_controller.rb:

def show 
  @books = @library.books.paginate(page: params[:books_page], per_page: 10)
end

libraries/show.haml:

#books_table
  = render "books_list", books: @books

libraries/show.js.erb:

$('#books_table').replaceWith('<%= j render "books_list", books: @books %>');

A 'books_list' partial has a pagination link which works fine:

books/_books_list.haml:

...
= paginate books, param_name: 'books_page'

When I add a new book to the list, #books_table is automatically replaced with _books partial like so (books/create.js.erb):

$('#books_table').replaceWith('<%= j render "libraries/books_list", books: @library_books%>')

books_controller.rb:

def create
  @library_book = @library.books.create(book_params)
  @library_books = @library.books.paginate(page: params[:books_page], per_page: 10)
end

But at this point pagination doesn't work. Any ideas how should I proceed?

Nadiya
  • 1,421
  • 4
  • 19
  • 34

0 Answers0