Reputation: 2254
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
Upvotes: 3
Views: 6350
Reputation: 7978
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
.
Upvotes: 5