嘿大家好,我正在开发一个应用程序,并且我在使用新的Rails 3 link_to时遇到了困难。这是情况 - 我的“主页”页面上有两个div,每个div都使用ajax填充到document.load中。这按预期工作。rails link_to:远离ajax加载的内容
在我加载到这些div的页面内容中,我想使用link_to ...:remote => true功能。该源按照预期与data-remote =“true”标记进行呈现,但是当我点击链接时,它们完全被忽略(该链接被视为常规超链接)。
我已经编写了能正确处理所有事情的.rjs文件(因为它们在硬编码时工作),所以这不是问题。
下面是阿贾克斯加载内容的HTML源:
<div>
<a href="/cart/add/2" data-remote="true">Link A</a>
<a href="/cart/add/1" data-remote="true">Link B</a>
</div>
点击时通常嵌入到网页中的链接之一,导致我的购物车,以正确地更新......然而,当我动态加载这个内容到AJAX页面,链接只是跟着忽略data-remote =“true”...
最初我以为它和原型没有正确加载AJAX的东西有关,但我'我已经检查了所有这一切,并没有改变一件事...
我真的很困惑...有没有人有这个想法?
# add method of controller
def add
product = Product.find_by_id(params[:product_id])
params[:quantity] ||= 1
params[:price] ||= product.price
unless product.nil?
@line = session[:cart].purchase_lines.select {|l| l.product_id == product.id}.first
unless @line.nil?
@line.quantity = (@line.quantity + params[:quantity].to_i)
else # create a new purchase_line in the cart
@line = session[:cart].purchase_lines.build({ :product_id => product.id, :price => params[:price], :quantity => params[:quantity].to_i })
end
else
flash[:error] = "Unable to add the selected product"
end
end
和我RJS
# add.rjs
page.if page['product_' << @line.product_id.to_s] do
page.replace 'product_' << @line.product_id.to_s, :partial => 'line'
page.else
page.insert_html :bottom, 'cart', :partial => 'line'
end
page.replace_html 'sub_total', number_to_currency(session[:cart].sub_total, :unit => '$')
page.replace_html 'tax', number_to_currency(session[:cart].tax, :unit => '$')
page.replace_html 'total', number_to_currency(session[:cart].sub_total+session[:cart].tax, :unit => '$')
page.visual_effect :highlight, 'product_' << @line.product_id.to_s, :duration => 1
我同意... jQuery更好,但我想尝试RJS/proto,因为我在看railscasts,并且它们看起来很容易 – sethvargo 2010-11-14 00:50:53