Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

449
Views
Rails - Ajax: ActionController::UnknownFormat

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!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 %>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!