3

I have a page, taxons#show, that lists a bunch of products, when you click a product it is suppose to take you to products#show, which is the individual product page. This page displays the partial _cart_form.html.erb, which has a bunch of functionality to add the product to cart, choose size, color, etc.

On Taxons#show, when you click a product, instead of going to the individual product page I would like to display a lightbox, on taxons#show, with the _cart_form.html.erb partial inside of it. I have the light box working, and when you click the image it correctly brings up the lightbox rather than go to the products#show page, but I'm not sure how to render the partial inside of it. Here's the link I have so far:

<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-->

And the lightbox code is a div that is display: none until clicked that looks like this:

<%# ******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 -->

Can anyone tell me how to render a partial when you click that link, inside of the lightbox? Thanks!

EDIT**

When I try to render the partial directly in the lightbox inside of taxons#show I get the the following NoMethod error:

 NoMethodError in Spree/taxons#show

 Showing    /home/telegraph/telegraph1/pants.telegraphbranding.com/app/views/spree/shared/_cart_form.html.erb where line #4 raised:

undefined method `has_variants?' for nil:NilClass
Extracted source (around line #4):

1: <%= form_for :order, :url => populate_orders_path do |f| %>
2:   <div id="inside-product-cart-form" data-hook="inside_product_cart_form"     itemprop="offers" itemscope itemtype="http://schema.org/Offer">
3: 
4:     <% if @product.has_variants? %>
5:       <div id="product-variants" class="columns five alpha">
6:         <h6 class="product-section-title"><%= t(:variants) %></h6>
7:         <ul>

[UPDATE 9/24/13]

The marked solution fixed the above problem but resulted in an awful stack level too deep error that resulted in me scrapping the functionality. But I think this is a good solution:

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

Community
  • 1
  • 1
reknirt
  • 2,237
  • 5
  • 29
  • 48
  • Is the lightbox hardcoded in to your page and some JS just toggles it? Or does all the lightbox content get returned from your app ajaxically? – Mike Campbell Mar 25 '13 at 15:17
  • Right now the light box is hardcoded with a JS toggle. Rails is pulling in a different product to each lightbox though. – reknirt Mar 25 '13 at 15:20

1 Answers1

5

The way I would do it is have your app render the lightbox AJAXically

products_controller.rb

def show
  @product = Product.find params[:id]
  respond_to :js
end

show.js.erb

$("[id^='product_popup_']").remove();
$(body).append("<%= escape_javascript(render 'product_lightbox', product: @product) %>");

_product_lightbox.html.erb

<%# ******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") %>
<%= render 'shared/cart_form' %>
</div><!-- popup-image -->

But in your question you don't actually make it clear what problem you're having. Have you tried just rendering the partial in the lightbox code? :/

EDIT - Based on your error, @product is nil. So before you render your cart partial, set <% @product = product %>, because your lightbox code looks like it uses product. Otherwise, just use @product from the controller downwards, then you can have views that render partials that all use @product.

EDIT again - Alternatively, pass the product object to your partial as a local, like: <%= render 'bla', :product => product %> and edit to partial to use product instead of @product.

Mike Campbell
  • 7,921
  • 2
  • 38
  • 51
  • I have tried rendering the partial, I get a bunch of NoMethod errors because the product model has a lot more model associations than the taxon model I'm trying to display the partial in. – reknirt Mar 25 '13 at 15:29
  • Try putting your actual error message in your question. It's not really any help at all just saying your getting a "bunch of NoMethod errors"! Associations shouldn't matter as long as you're working with the correct objects.. – Mike Campbell Mar 25 '13 at 15:32
  • NoMethodError in Spree/taxons#show Showing /home/telegraph/telegraph1/pants.telegraphbranding.com/app/views/spree/shared/_cart_form.html.erb where line #4 raised: undefined method `has_variants?' for nil:NilClass Extracted source (around line #4): 4: <% if @product.has_variants? %> 5:
    6:
    <%= t(:variants) %>
    7:
      – reknirt Mar 25 '13 at 15:34
    • Thank you so muchh! The <% @product = product %> solved the problem! – reknirt Mar 25 '13 at 16:23
    • Sorry...one last question. When I do `<% @product = product %>` it changes the main image for each product on the taxons#show page to the same image, since each product is using `product` to pull the image. Also, when I put the lightbox into a partial and call that I get a "stack level too deep" error? – reknirt Mar 25 '13 at 16:54
    • I wrote a general post on using AJAX with Rails 4 to render a partial view in response to a form post: http://stackoverflow.com/questions/26808696/rails-4-rendering-a-partial-with-ajax-jquery-remote-true-and-respond-to/26809518#26809518 – emery Nov 07 '14 at 20:26