I have a form for filtering products, and want to change by AJAX the results on the partial for results:
<%= form_for :pin, url: filter_path, method: :get, remote: true do |f| %>
<%= f.radio_button :category, category %>
<%= f.range_field :max_value , type: "range", value: "1000", min: "1", max: "1000" %>
<%= f.submit "Buscar", class: "margin_top width100" %>
<% end %>
In the controller, I respond to JS with a partial:
respond_to do |format|
format.js { render "filter_products" }
format.html {render "index"}
end
And lastly, on my filter_products.js
, I change the partial with the new results:
$(document).ready(function(){
var newProducts = '<%= j(render partial: "product_list", collection: @pins) %>';
$("#product-list").replaceWith(newProducts);
});
Where product_list is my partial with the new products:
<% @pins.each do |pin| %>
<%= render partial: "product_card", locals: {pin: pin} %>
<% end %>
Its working OK, but only the first time I submit the form. The second time on, its sending XHR requests with 200 responses, but it's not replacing the old products on the view.
Am I missing something?
Thanks!