I'm having trouble for this button:
controller
class ProductsController < ApplicationController
def test
@product = Product.find(params[:my][:id])
@k = params[:my][:k]
@product.update_attribute :amount, @product.amount + @k.to_i
respond_to do |format|
format.js
format.html
end
end
def product_params
params.require(:product).permit(:amount)
end
view:
<% @products.each do |product| %>
<td class="ajax">
<%= product.amount %>
</td>
<td><%= link_to '+1', {controller: 'products', action: 'test',
my: {id: product.id, k: '1'}},
:method => :post, :remote => true,
class: "btn btn-info btn-xs" %>
</td>
test.js.erb:
$(".ajax").html("<%= @product.amount %>")
routes:
resources :products
post 'products/test'
when I press the button, every product is updated with the value of product corrisponding to the button, refreshing the page every value is correct. Someone can help me?
I bet all of your products have css class ajax
. Try to use id instead.
Edit:
Your view:
<% @products.each do |product| %>
<td id="product-<%= product.id %>">
<%= product.amount %>
</td>
<td>
<%= link_to '+1', {controller: 'products', action: 'test',
my: {id: product.id, k: '1'}},
:method => :post, :remote => true,
class: "btn btn-info btn-xs" %>
</td>
<% end %>
test.js.erb:
$("#product-<%= @product.id %>").html("<%= @product.amount %>")