Rails 3 ajax 更新部分在没有页面刷新的情况下不起作用
我正在开发 Rails 3 购物车。我有一个目录页面,其中显示所有产品,还有一个部分显示购物车的要点。因此,用户添加到购物车的任何产品都会显示在此购物车部分中。现在,这不起作用了。将产品添加到购物车后,我必须点击刷新,否则部分不会更新。
这是我的代码:
Cart_Controller add 方法
def add
@cart = user_cart
@deal = Deal.find(params[:id])
@item = @cart.add(params[:id])
flash[:notice] = "已将 #{@deal.deal_name} 添加到购物车"
@cart = user_cart
respond_to do |格式|
format.html{redirect_to root_path}
format.js
end
add.js.erb
$("#shopping_cart").replace_html("<%=escape_javascript(render(:partial => 'cart', :locals => { :cart => @cart}))%>");
产品页面
<%=渲染:部分=> '购物车/购物车',
<代码>:当地人=> {:购物车=> user_cart}%>
<%end%>
<%=link_to "添加到购物车",{:action => '添加', :id => deal.id}, :remote => true%>
这是我在日志中看到的内容:
Started GET "/cart/add/2" for 127.0.0.1 at 2011-07-02 18:04:38 -0700
CartController处理#add as JS
参数:{"id"=>"2"}
渲染的 cart/_cart.html.erb (9.6ms)
渲染 cart/add.js.erb (11.7ms)
在 274 毫秒内完成了 200 项任务(查看次数:23.4 毫秒 | ActiveRecord:0.0 毫秒)
请帮我解决这个问题。
I'm working on a Rails 3 shopping cart. I've a catalog page that displays all products and one partial that gives a gist of the shopping cart. So whatever product a user adds to the cart is displayed in this shopping cart partial. Now, this ain't working. I've to hit refresh after adding a product to cart, otherwise the partial doesn't update.
Here's my code:
Cart_Controller add method
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}))%>");
Products page
<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%>
This is what I see in my logs:
Started GET "/cart/add/2" for 127.0.0.1 at 2011-07-02 18:04:38 -0700
Processing by CartController#add as JS
Parameters: {"id"=>"2"}
Rendered cart/_cart.html.erb (9.6ms)
Rendered cart/add.js.erb (11.7ms)
Completed 200 OK in 274ms (Views: 23.4ms | ActiveRecord: 0.0ms)
Please help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
Try