Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 渲染:部分未按预期工作_Ruby On Rails 4_Renderpartial - Fatal编程技术网

Ruby on rails 4 渲染:部分未按预期工作

Ruby on rails 4 渲染:部分未按预期工作,ruby-on-rails-4,renderpartial,Ruby On Rails 4,Renderpartial,在我的产品索引视图中,我有一个“添加到购物车”调用javascript函数addToCart: addToCart: function() { $.ajax({type: 'GET', url: 'store/add_to_cart/2', // fixed id of product timeout: 5000, success: function() { alert('Added !'); }, error:

在我的产品索引视图中,我有一个“添加到购物车”调用javascript函数addToCart:

addToCart: function() {
  $.ajax({type:     'GET',
          url:  'store/add_to_cart/2',    // fixed id of product
      timeout:  5000,
      success:  function() { alert('Added !'); },
      error:    function() { alert('Error !'); }
  });
}



 def add_to_cart  // not working
    begin
      prod = Product.find(params[:id])
      @cart = find_cart
      @cart.add_product(prod)
      render :partial => 'cart', :object => @cart if request.xhr? 
    end
  end
使用此add_to_cart,它会呈现部分视图,但也会呈现此方法的默认视图-add_to_cart.html.haml- 但是如果我像下面这样做,它只渲染部分。 有人能解释一下为什么不同吗

 def add_to_cart    // working fine
    begin
      prod = Product.find(params[:id])
      @cart = find_cart
      @cart.add_product(prod)
      if request.xhr?
        render :partial => 'cart', :object => @cart 
      else
        redirect_to_index    
      end
    end
  end

谢谢你的帮助

问题在于,在该行中,rails get混淆了render调用的参数和语句的位置。 如果request.xhr?,您可能应该尝试使用
呈现(:partial=>'cart',:object=>@cart)

还有一件事。如果在分部中使用局部变量,最好使用
locals:{cart:@cart}
而不是
:object
。或者,如果您遵循约定,并且
cart
partial位于
app/view/carts/\u cart.html*
中,您可以说
render@cart

我尝试了
如果请求,则渲染(:partial=>'cart',:object=>@cart)。xhr?
但是相同:渲染部分和默认值。还有别的想法吗?