0

I am using the gem Kaminari with AJAX. The AJAX part is working fine. However, when I say in my controller .per(3) it returns me every object times 3. So when I have 9 recipes, it returns me 27 recipes (making duplicates). I only get unique objects when I say .per(1), but I don't want a new page for every single object.

Please help me with this issue. Thank you in advance.

My controller:

class RecipesController < ApplicationController
  skip_before_action :authenticate_user!
  def index
    if params[:search]
      @recipes = Recipe.search(params[:search]).order('created_at DESC').page(params[:page]).per(3)
    else
      @recipes = Recipe.order('created_at DESC').page(params[:page]).per(3)
    end
  end
end

My view:

<div class="container is-medium" id="product-recipe">
  <%= render @recipes %>
</div>

<% if @recipes.present? %>
  <div class="apple_pagination" id="paginator">
    <%= paginate @recipes, :remote => true %>
  </div>
<% end %>

index.js.erb:

$('#product-recipe').html('<%= escape_javascript render(@recipes) %>');
$('#paginator').html('<%= escape_javascript(paginate(@recipes, :remote => true).to_s) %>');
Seifer
  • 247
  • 3
  • 10

1 Answers1

0

I found the solution. For the people who may come across the same problem in the future.

In my recipe partial I had @recipes.each do |recipe| .... That was the problem, removing that line fixed the duplicate problem.

Seifer
  • 247
  • 3
  • 10