I am trying to build an ecommerce site using RoR. I have this logic where the user can add more or less quantity to the product they would like to purchase:
<%= link_to order_item_path(item, 'minus'), remote: true, method: :patch do %>
<button>-</button>
<%end %>
<%= item.quantity %>
<%= link_to order_item_path(item, 'plus'), remote: true, method: :patch do %>
<button>+</button>
<%end %>
So every time they click on plus
or minus
, a patch
call is sent to the OrderItems controller:
def update
#do some logic
respond_to do |format|
format.js
format.html { redirect_to new_charge_path }
end
end
In the controller we do some logic and I donĀ“t want the page to refresh but to use JavaScript to make the number go up or down. I want to do that using js.erb
:
#order_items/update.js.erb
console.log("plus minus")
#here make number go up or down
The problem is that when the user clicks the button of plus or minus, I get an error:
ActionController::UnknownFormat in OrderItemsController#update
respond_to do |format|
format.js
format.html { redirect_to new_charge_path }
end
Anyone knows why it is happening and how could I solve it? Thanks!