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
The logs show this:
#it does all the logic of the `update` method and when it arrives to the `respond_to` it gives the following:
web_1 | Completed 406 Not Acceptable in 322ms (ActiveRecord: 47.4ms | Allocations: 32091)
web_1 |
web_1 |
web_1 |
web_1 | ActionController::UnknownFormat (ActionController::UnknownFormat):
web_1 |
web_1 | app/controllers/order_items_controller.rb:44:in `update'
Anyone knows why it is happening and how could I solve it? Thanks!
Solved it!
So this:
<%= 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 %>
Passes plus
and minus
to the controller like this:
{"id"=>"90", "format"=>"plus"}
I guess the format
on the params was conflicting with the format
here:
ActionController::UnknownFormat in OrderItemsController#update
respond_to do |format|
format.js
format.html { redirect_to new_charge_path }
end
So I just changed it to this:
<%= link_to order_item_path(item, operator: 'minus'), remote: true, method: :patch do %>
<button>-</button>
<%end %>
<%= item.quantity %>
<%= link_to order_item_path(item, operator: 'plus'), remote: true, method: :patch do %>
<button>+</button>
<%end %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.