解决:增加AJAX按钮轨 [英] Solved: increasing ajax button rails

查看:114
本文介绍了解决:增加AJAX按钮轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为这个按钮:

控制器

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

查看:

<% @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:

test.js.erb:

$(".ajax").html("<%= @product.amount %>")

路线:

resources :products
post 'products/test'

当我preSS的按钮,每一件产品都与产品corrisponding到按钮的值更新,刷新页面的每一个值是正确的。有人能帮助我吗?

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?

推荐答案

我敢打赌,你的所有产品都有CSS类 AJAX 。尝试使用ID来代替。

I bet all of your products have css class ajax. Try to use id instead.

编辑:

您的看法:

<% @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:

test.js.erb:

$(#产品 - &LT;%= @ p​​roduct.id%&gt;中)。HTML(&LT;%= @ p​​roduct.amount%&gt;中)

这篇关于解决:增加AJAX按钮轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆