Can't find root of 'stack level too deep' error when rendering partial

佐手、 提交于 2020-01-15 04:24:21

问题


I've been stuck on this problem for a week. I will mail you a bottle of scotch if you can figure it out. Seriously, its come to bribery.

On taxons#show I'm rendering a products partial, _products.html.erb, which lists out all the products on a table to the show view of the taxons controller. When you click a product, by default the app will redirect the user to products#show, where the _cart_local.html.erb partial is rendered to display 'add to cart' options.

But on taxons#show, when a product is clicked I bring up a lightbox instead so the user doesn't have to leave the page. The lightbox code is inside _products.html.erb, and I'm trying to render _cart_form.html.erb inside of the lightbox. When I do, I get the 'stack level too deep' error and taxons#show won't render.

But the cart renders fine in products#how. I changed @product in the partial to just product. That didn't help. I rendered an empty partial and the page loads, which makes me think the problem is with _cart_local (but why would it render on products#show?).

Then I took out all of the code in between the opening form tag and the ending div/end tags and the page also rendered, which makes me think its in that block, but I can't wittle it down any further. I'm stuck

Here's the code for _cart_local, and if I take out the code between the <!-- Here --> and <!-- AND HERE --> comments, the page renders:

<%= form_for :order, :url => populate_orders_path do |f| %>
<div id="inside-product-cart-form" data-hook="inside_product_cart_form" itemprop="offers" itemscope itemtype="http://schema.org/Offer">

<% if product.has_variants? %>  <!-- HERE --> 
  <div id="product-variants" class="columns five alpha">
    <h6 class="product-section-title"><%= t(:variants) %></h6>
    <ul>
      <% has_checked = false
      product.variants.active(current_currency).each_with_index do |v,index|
        next if v.option_values.empty? || (!v.in_stock && !Spree::Config[:show_zero_stock_products])
        checked = !has_checked && (v.in_stock || Spree::Config[:allow_backorders])
        has_checked = true if checked %>
        <li>
          <%= radio_button_tag "products[#{product.id}]", v.id, checked, :disabled => !v.in_stock && !Spree::Config[:allow_backorders], 'data-price' => v.price_in(current_currency).display_price %>
          <label for="<%= ['products', product.id, v.id].join('_') %>">
            <span class="variant-description">
              <%= variant_options v %>
            </span>
            <% if variant_price v %>
              <span class="price diff"><%= variant_price v %></span>
            <% end %>
          </label>
        </li>
      <% end%>
    </ul>
  </div>
<% end%>

<% if product.price_in(current_currency) and !product.price.nil? %>  
  <div data-hook="product_price" class="columns five <% if !product.has_variants? %> alpha <% else %> omega <% end %>">

    <div id="product-price">
      <h6 class="product-section-title"><%= t(:price) %></h6>
      <div><span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span></div>
    </div>

    <div class="add-to-cart">
      <% if product.on_sale? %>      
        <%= number_field_tag (product.has_variants? ? :quantity : "variants[#{product.master.id}]"),
          1, :class => 'title', :in => 1..product.on_hand, :min => 1 %>
        <%= button_tag :class => 'large primary', :id => 'add-to-cart-button', :type => :submit do %>
          <%= t(:add_to_cart) %>
        <% end %>
      <% else %>
        <%= content_tag('strong', t(:out_of_stock)) %>
      <% end %>
    </div>
  </div>
<% else %>
    <div id="product-price">
      <br>
      <div><span class="price selling" itemprop="price"><%= t('product_not_available_in_this_currency') %></span></div>
    </div>
<% end %> <!-- AND HERE -->   

</div>
<% end %>

And here is _products.html.erb, the file that is loading all the products, contains the lightbox, and has the render cart partial code:

<div class="overlay-container">
</div>
<%
 paginated_products = @searcher.retrieve_products if params.key?(:keywords)
 paginated_products ||= products
%>
<% if products.empty? %>
 <%= t(:no_products_found) %>
<% elsif params.key?(:keywords) %>
 <h6 class="search-results-title"><%= t(:search_results, :keywords =>  h(params[:keywords])) %></h6>
 <% end %>
 <div class="product_grid_container">
 <div class="grid_2"><%= image_tag("store/featured/#{@featured}.jpg") %></div>
 <% if products.any? %>
  <ul id="products" class="inline product-listing" data-hook>
  <% products.each do |product| %> 
   <% if product.on_display? %>
    <%# ******LIGHTBOX******* %>
     <div id="product_popup_<%= product.id %>" class="product_popup" data-popid="<%= product.id %>">
        <div class="related-products">
          <ul class="related_products_list" id="related_products_list_<%= product.id %>" data-listid="<%= product.id %>">
         <% @related_products.each do |related_product| %>
         <li class="related_products_item"><%= link_to large_image(related_product, :itemprop => "image", :data => {:imageid => related_product.id}, :id => "related_" + related_product.id.to_s, :class => "related_products_image dimmed"), url_for(related_product) %></li>
         <% end %>
    </ul>
        </div>
        <div class="popup-image">
        <%= large_image(product, :itemprop => "image", :class => "product-image-popup") %>
        </div><!-- popup-image -->
        <div class="popup_right_content">
      <h2 class="popup-title"><%= product.name %></h2>
      <p class="popup-price">$<%= product.price %></p>
      <p><%= product.description %></p>
      <p class="popup-color">color:</p>
      <div class="popup-images" data-productid="<%= product.id %>">
            <% if (product.images + product.variant_images).uniq.size > 1 %>
            <ul id="popup-thumbnails-taxon" class="thumbnails inline" data-hook>
            <% product.images.each do |i| %>
                <li class='tmb-all' id='tmb-<%= i.id %>'>
                    <%= link_to(image_tag(i.attachment.url(:small)), i.attachment.url(:popup), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
                </li>
            <% end %>
            </ul>
        <% end %>
    </div><!-- popup-images -->
    </div><!-- popup_right_content -->
        <%= render 'spree/shared/cart_local', :locals => {:product => product} %>
 </div><!-- product_popup -->   
    <%# ******END LIGHTBOX******* %>
  <div class="grid_1">
  <li id="product_<%= product.id %>" class="columns product three <%= cycle("alpha", "secondary", "", "omega secondary", :name => "classes") %>" data-hook="products_list_item" itemscope itemtype="http://schema.org/Product">
    <div class="main-image" id="single_<%= product.id %>" data-productid="<%= product.id %>">

      <%= link_to large_image(product, :itemprop => "image", :class => "product-image", :id => product.id), product_path(product), :remote => true, :html => {:class => "product_popup"}  %>
    </div><!-- main-image-->
    <div class="prod_info_box">
    <%= link_to truncate(product.name, :length => 50), product, :class => 'info', :itemprop => "name", :title => product.name %>
    <span class="price selling" itemprop="price"><%= product.price_in(current_currency).display_price %></span>

    <!-- BRINGS THUMBNAILS INTO TAXONS PAGE -- PULLED FROM _THUMBNAILS.HTML.ERB -->
    <div class="product-images" data-productid="<%= product.id %>">
    <% if (product.images + product.variant_images).uniq.size > 1 %>
        <ul id="product-thumbnails-taxon" class="thumbnails inline" data-hook>
            <% product.images.each do |i| %>
                <li class='tmb-all' id='tmb-<%= i.id %>'>
                    <%= link_to(image_tag(i.attachment.url(:mini)), i.attachment.url(:normal), :class => 'tmb-all', :id => "tmb-#{i.id}") %>
                </li>
            <% end %>
        </ul>
    <% end %>
    </div><!-- product-images -->
    <!-- END THUMBNAILS INTO TAXONS PAGE -->
    <div id="product-description-taxon">
        <p><%= product.description %></p>
    </div><!-- product-description-taxon -->
    </div><!-- prod_info_box -->
  </li>
  </div>
<% end %>
<% end %>

 <% reset_cycle("classes") %>
 </ul>
 <% end %>
 </div><!-- product_grid_container -->
 <% if paginated_products.respond_to?(:num_pages) %>
 <%= paginate paginated_products %>
 <% end %>

Let me know if you need anything else. i appreciate it.

Here's a link to the helpers, maybe the problem is there? https://github.com/spree/spree/tree/v1.3.2/core/app/helpers/spree


回答1:


In your _products.html.erb partial change that:

<%= render 'spree/shared/cart_local', :locals => {:product => product} %>

to that:

<%= render partial: 'spree/shared/cart_local', :locals => {:product => product} %>

and problem should be resolved.

Why? Because doing it as you have done it won't pass locals into partial and that's why you've received error, check it by yourself by removing locals. Of course the most interesting part is why you get stack level too deep error here, but I am unable to find answer for that now.

Oh, and debugger is your friend ;)



来源:https://stackoverflow.com/questions/15669022/cant-find-root-of-stack-level-too-deep-error-when-rendering-partial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!