增加Ajax按钮导轨 [英] Increasing ajax button rails

查看:75
本文介绍了增加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'

当我按下按钮时,每个产品都会更新与该按钮对应的产品值,刷新页面时每个值都是正确的.有人可以帮我吗?

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:

$(#product-<%= @ product.id%>").html(<%= @ product.amount%>")

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

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