Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda