weixin_33713503 2014-02-05 13:28 采纳率: 0%
浏览 30

为什么我的Ajax请求没有被用于向Cart中添加条目?

我到处都找过了,但似乎找不到为什么我的Ajax请求没有被用于向Cart中添加条目的问题。

line_items_controller.rb

def create
  product = Product.find(params[:product_id])
  @line_item = @cart.add_product(product.id)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to store_url }
      format.js   { @current_item = @line_item }
      format.json { render action: 'show',
                           status: :created, location: @line_item }
    else
      format.html { render action: 'new' }
      format.json { render json: @line_item.errors,
                           status: :unprocessable_entity }
    end
  end
end

store/index.html.erb

       <% if notice %>
<p id="notice"><%= notice %></p>
<% end %>

<h1>Your Pragmatic Catalog</h1>

<% cache ['store', Product.latest] do %>
<% @products.each do |product| %>
    <% cache ['entry', product] do %>
        <div class="entry">
          <%= image_tag(product.image_url) %>
          <h3><%= product.title %></h3>
          <%= sanitize(product.description) %>
          <div class="price_line">
            <span class="price"><%= number_to_currency(product.price) %></span>
            <%= button_to 'Add to Cart', line_items_path(product_id: product),
                          remote: true %>
          </div>
        </div>
    <% end %>
<% end %>
<% end %>

line_items/create.js.erb

$('#cart').html("<%= escape_javascript render(@cart) %>");

$('#current_item').css({'background-color':'#88ff88'}).
    animate({'background-color':'#114411'}, 1000);

line_items/_line_item.html.erb

 <% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td> <%= line_item.quantity %>&times; </td>
<td> <%= line_item.product.title %> </td>
 <td class= "item_price" > <%= number_to_currency(line_item.total_price) %> </td>
</tr>

可以在日志文件中看到,每当我向购物车Cart添加一个项目时,页面就会发送一堆GET请求,而且我还可以看到它重新加载,但日志文件中没有发现错误。我多次重新启动该网站,并检查是否正确安装了jQuery和jQuery-UI gems,但什么作用都没有。

  • 写回答

4条回答 默认 最新

  • weixin_33725807 2014-02-05 13:45
    关注

    try specifying

    method: :post

    in the button_to call. A get request isn't going to trigger the create method.

    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码