So, in case you have been stuck in this for hours (like I was), the problem is that you must surround j render statement with double quotes
cart_items.innerHTML = <%= j render 'cartItems'%>
should be
cart_items.innerHTML = "<%= j render 'cartItems'%>"
I have a cart that contains items, in the checkout page these items can be reviews and their quantity cna be changed. I want to add a button that, when clicked, will refresh the partial that is rendering the cart thus updating totals etc.
I have created the button and it changes the innerHtml its supposed to (when I test it with (“test”) instead of the <% j render…%>, however it will not update the cart (reload the partial).
Heres my code
Route
get 'refresh_cart', to: 'carts#refresh'
Controller
def refresh
@cart = Cart.find_by(user_id: current_user.id)
respond_to do |format|
format.js {}
end
end
html
<div class="container-fluid cart-submit" id="cart-item-partial">
<%= render 'cartItems' %>
</div>
refresh.js.erb
cart_items = document.getElementById("cart-item-partial")
cart_items.innerHTML = <%= j render 'cartItems'%>
Also, when i click the “refresh” button I get a call to the server. “` tarted GET “/refresh_cart” for ::1 at 2021-01-15 11:00:23 -0300 Processing by CartsController#refresh as JS …. ↳ app/helpers/products_helper.rb:22:in `quantity_helper’ Rendered carts/_cartSubtotal.html.erb (Duration: 0.1ms | Allocations: 30) Rendered carts/_cartItems.html.erb (Duration: 32.7ms | Allocations: 10441) Rendered carts/refresh.js.erb (Duration: 34.3ms | Allocations: 11239) Completed 200 OK in 42ms (Views: 33.8ms | ActiveRecord: 3.3ms | Allocations: 1) “` However the page does not change