使用ajax调用渲染rails部分,并使用remote => true

时间:2022-06-01 06:55:34

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.

我有一个页面,taxons#show,列出了一堆产品,当你点击一个产品时,它会把你带到产品#show,这是个别的产品页面。此页面显示部分_cart_form.html.erb,它具有一系列功能,可将产品添加到购物车,选择尺寸,颜色等。

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:

在Taxons #show上,当你点击一个产品时,我想在单独的产品页面上显示一个灯箱,而不是在#cons_form.html.erb部分里面显示一个灯箱。我有灯箱工作,当你点击图像时,它正确地打开灯箱而不是去产品#show页面,但我不知道如何渲染部分内部。这是我到目前为止的链接:

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

灯箱代码是一个显示的div:在点击之前没有,如下所示:

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

当我尝试直接在taxons内部的灯箱中渲染部分时#show我得到以下NoMethod错误:

 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

渲染部分时无法找到“堆栈级太深”的根

1 个解决方案

#1


5  

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

我这样做是让你的应用程序呈现AJAX的灯箱

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.

编辑 - 根据您的错误,@ product是零。因此,在您将购物车部分渲染之前,请设置<%@ product = product%>,因为您的灯箱代码看起来像是使用产品。否则,只需从控制器向下使用@product,然后您就可以拥有渲染部分全部使用@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.

再次编辑 - 或者,将产品对象作为本地传递给您的部分,例如:<%= render'bla',:product => product%>并编辑为部分使用产品而不是@product。

#1


5  

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

我这样做是让你的应用程序呈现AJAX的灯箱

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.

编辑 - 根据您的错误,@ product是零。因此,在您将购物车部分渲染之前,请设置<%@ product = product%>,因为您的灯箱代码看起来像是使用产品。否则,只需从控制器向下使用@product,然后您就可以拥有渲染部分全部使用@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.

再次编辑 - 或者,将产品对象作为本地传递给您的部分,例如:<%= render'bla',:product => product%>并编辑为部分使用产品而不是@product。