【问题标题】:ajax not working on rails 4ajax 不能在 Rails 4 上运行
【发布时间】:2015-08-08 04:40:54
【问题描述】:

我到处寻找,但似乎找不到为什么我的 AJAX 请求没有通过将商品添加到购物车的问题。

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

存储/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>

我可以在我的日志文件中看到,每次我将商品添加到购物车时,该页面都会发送一堆 GET 请求,并且我可以看到它重新加载。在日志文件中没有发现错误。

多次重启网站。检查我是否正确安装了 jquery 和 jquery-ui gem。没有任何改变。

【问题讨论】:

    标签: jquery ruby-on-rails ajax


    【解决方案1】:

    尝试指定

    方法: :post

    在 button_to 调用中。 get 请求不会触发 create 方法。

    【讨论】:

      【解决方案2】:

      您正在调用 line_items_controller.rb 的创建操作,而创建操作是一个 POST 操作。

      所以在此添加method: :post

      <%= button_to 'Add to Cart', line_items_path(product_id: product), method: :post
                                remote: true %>
      

      并尝试在它调用或不调用的创建操作中进行调试。

      【讨论】:

      • 我试过发帖,但没有帮助。我并不是说我看到了针对该特定操作的 GET 请求。我的意思是通过向图像、样式表等发出 GET 请求,页面总是会再次呈现。我可以看到创建请求是发布的。它只是不使用 AJAX
      • 页面上是否存在“#cart”div?
      【解决方案3】:

      在部分顶部包含这两个 JavaScript 文件:

      javascript_include_tag "jquery", "jquery_ujs"
      

      【讨论】:

        【解决方案4】:

        您似乎忘记在create 操作中定义@cart。 只需添加@cart = current_cart

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多