基于'Agile Web Development with Rails,第三版',我用 Ajax 创建了一个购物车,但 Ajax 不适合我。我的代码如下:
/store/index.html.erb:</p>
<%= form_tag({:action=>'add_to_cart', :id=>product}, :remote=>true ) do %>
<%= submit_tag "Add to Cart" %>
<% end %>
/layouts/store.html.erb:</p>
<head>
...
<%= javascript_include_tag :defults %>
...
</head>
/controllers/store_controller.rb:</p>
def add_to_cart
...
respond_to do |format|
format.js
end
...
end
add_to_cart.js.rjs
page.replace_html("cart",:partial => "cart", :object=>@cart)
导轨 3.1,红宝石 1.9.3。