weixin_33733810 2015-06-02 09:46 采纳率: 0%
浏览 4

Ajax在Rails和渲染中

I'm learning about ajax, but even the first applications I write it remains inactive. After clicking the link, nothing changes.

cart_controller.rb

def cart_add_quan
        id_cart = params[:id]
        cart = session[:cart]
        cart.each do |id, quantity|
            if id_cart == id then
                cart[id] += 1
            end
        end
        respond_to do |format|
            format.html {redirect_to cart_path}
            format.js
        end
    end

views/cart/index.html.erb

<h1>Your cart:</h1>
<td><%= link_to 'Continue shopping', product_user_index_path %></td>
<% if @cart.empty? %>
    <p>Your cart is empty</p>
<% else %>
<div id="carts">
    <%= render 'carts' %>
    <br>
        <h2><%= link_to 'Check out', check_out_index_path %></h2>
    <br>
    <td><%= link_to 'Empty your cart', cart_clear_path %></td>
</div>
<% end %>

views/cart/_carts.html.erb

<br>
    <table>
        <thead>
            <tr>
                <th>Product name</th>
                <th>Image</th>
                <th>Description</th>
                <th>SubPrice</th>
                <th>Quantity</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <br>
            <% @cart.each do |id, quantity| %>
            <% product = Product.find_by_id(id) %>

            <tr>
                <td><%= link_to product.name, product %></td>
                <td><%= image_tag(product.image.thumb('45x45#').url, class: "SlideItMoo_element") if product.image_stored? %></td>
                <td><%= product.description.html_safe %></td>
                <td><%= sub_price(product, quantity) %></td>
                <td>
**#link to *cart_quan_path***
                    <%= link_to "+", cart_add_quan_path(id), remote: true %> 
                    <%= quantity %>
                    <%= link_to "-", cart_sub_quan_path(id) %></td>
                <td><%= link_to 'remove', cart_remove_product_path(id) %></td>
            </tr>
            <% end %>
        </tbody>    
    </table>
<h3 id ="total">Totald: <%= totalCart(@cart) %></h3>

views/cart/cart_add_quan.js.erb

$('#carts').html('<%= escape_javascript(render("carts")) %>');
console.log("hihi");
  • 写回答

0条回答 默认 最新

    报告相同问题?