轨道4 AJAX更新DIV [英] Rails 4 ajax update div

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

问题描述

我想更新表使用AJAX的新值。 是什么概念。我有一个车,其中,用户可以改变产品的数量。我不喜欢重新加载整个页面。我想重装表。 目前,它与重新加载整个页面。

I like to update a table with the new values using ajax. What is the idea. I have a cart, where the user can change the quantity of the product. I don't like to reload the whole page. I want to reload the table. At the moment it works with reloading the whole page.

我做了什么。

我的输入形式的量值。要与车连接的产品(多对多)我使用line_item。所以每line_item重新presents产品在购物车中。

I have a form for input a quantity value. To connect the product with a cart (ManyToMany) I uses line_item. So every line_item represents a product in a cart.

此观点的购物车中

<div id="cart_table">
  <%= render @cart %>
</div>

渲染@cart

The render @cart

<table class="table  table-hover table-condensed">
  <tbody>
    <%= render cart.line_items %>
  </tbody>
</table>

Line_items与更新形式:

Line_items with the update form:

<%= form_tag(line_item_path, method: "put", remote: true, class: "table_form") do %>
  <%= hidden_field_tag("product_id", value = line_item.product.id) %>
  <label>
    <%= number_field_tag "quantity", value = line_item.quantity, in: 1...11 %>
    <%= submit_tag "update", class: "btn btn-mini" %>
  </label>
<% end %>

line_items的控制器:

The controller of line_items:

def update
    @cart = current_cart
    @line_item = @cart.update_product(params[:product_id], params[:quantity])
    respond_to do |format|
        if @line_item.save
            format.html { redirect_to current_cart, notice: 'Changed' }
            format.js
        else
            format.html { render action: "new" }
        end
    end
end

下面是我卡住。 我必须把我的update.js.rjs?现在我有这行

Here is where I get stuck. What I have to put in my update.js.rjs? Now I have this line

$('#cart_table').replaceWith("<%= escape_javascript(render @cart) %>");

但什么也没有发生,我也得到了控制台错误:

But nothing happens and I get an error in the console:

send 
jQuery.extend.ajax 
$.rails.rails.ajax 
$.rails.rails.handleRemote 
(anonymous function) 
jQuery.event.dispatch 
elemData.handle

将是很好的得到一些想法来解决这个小问题。

Would be nice to get some ideas to solve this little problem.

感谢

编辑:这是更好地使用

$('#cart_table').html("<%= escape_javascript(render @cart) %>");

在js文件。

推荐答案

尝试使用 update.js.erb ,而不是 update.js的。 RJS

这篇关于轨道4 AJAX更新DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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