【问题标题】:Rails 3 ajax update partial doesnt work without page refreshRails 3 ajax 更新部分在没有页面刷新的情况下不起作用
【发布时间】:2011-07-03 01:39:05
【问题描述】:

我正在开发 Rails 3 购物车。我有一个显示所有产品的目录页面和一个部分显示购物车的要点。因此,用户添加到购物车的任何产品都会显示在此购物车部分中。现在,这行不通。将产品添加到购物车后我必须点击刷新,否则部分不会更新。

这是我的代码:

Cart_Controller 添加方法

def add
@cart = user_cart
@deal = Deal.find(params[:id])
@item = @cart.add(params[:id])
flash[:notice] = "Added #{@deal.deal_name} to cart"
@cart = user_cart
respond_to do |format|
format.html{ redirect_to root_path}
format.js
end

add.js.erb

$("#shopping_cart").replace_html("<%=escape_javascript(render(:partial => 'cart', :locals => {:cart => @cart}))%>");

产品页面

<div id="shopping_cart">
<%=render :partial => 'cart/cart',
:locals => {:cart => user_cart}%>
<%end%>
</div>

<%=link_to "Add to Cart",{:action => 'add', :id => deal.id}, :remote => true%>

这是我在日志中看到的:

在 2011-07-02 18:04:38 -0700 开始为 127.0.0.1 获取“/cart/add/2”
由 CartController#add as JS 处理
参数:{"id"=>"2"}
渲染的购物车/_cart.html.erb (9.6ms)
渲染的购物车/add.js.erb (11.7ms)
在 274 毫秒内完成 200 次 OK(查看次数:23.4 毫秒 | ActiveRecord:0.0 毫秒)

请帮帮我。

【问题讨论】:

  • 你使用prototype还是jquery?

标签: ajax ruby-on-rails-3 renderpartial


【解决方案1】:

试试

$("#shopping_cart").html("<%= escape_javascript(render(:partial => 'cart', :locals => {:cart => @cart})).html_safe %>");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-02
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2016-11-29
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多