Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 在渲染中渲染?使用render@product呈现@category_Ruby On Rails_Ruby_Render - Fatal编程技术网

Ruby on rails 在渲染中渲染?使用render@product呈现@category

Ruby on rails 在渲染中渲染?使用render@product呈现@category,ruby-on-rails,ruby,render,Ruby On Rails,Ruby,Render,我目前有一个菜单/商店首页,显示类别以及分配给该类别的产品下面 但是,我也需要将其更改为render@类别和render@产品,但我不确定如何将它们链接起来,并为每个类别呈现分配给该类别的产品 如果有人能告诉我这将如何工作,我将非常感激 查看 <body class="font-sans container"> <div class="w-full md:w-3/5 mx-auto p-8"> <

我目前有一个菜单/商店首页,显示类别以及分配给该类别的产品下面

但是,我也需要将其更改为render@类别和render@产品,但我不确定如何将它们链接起来,并为每个类别呈现分配给该类别的产品

如果有人能告诉我这将如何工作,我将非常感激

查看

 <body class="font-sans container">
      <div class="w-full md:w-3/5 mx-auto p-8">
         <p> <strong>Drink</strong></p>
         <div class="shadow-md">
          <% cache @categories do %>
            <% @categories.where(user_id: @account.id).each do |category| %>
              <% cache category do %>
            <div class="tab w-full overflow-hidden border-t">
               <input class="absolute opacity-0 " id="tab-multi-<%= category.category%>" type="checkbox" name="tabs">
               <label class="block p-5 leading-normal cursor-pointer" for="tab-multi-<%= category.category%>"> <%= category.category%> </label>
                <% cache @products do %>
                <% @products.where(user_id: @account.id, category: category, available: 't').each do |product| %>
                <% cache product do %>
               <div class="tab-content overflow-hidden border-l-2 bg-gray-100 border-indigo-500 leading-normal">
                 <ul class="flex flex-col p-2 ">
                    <li class="border-gray-400 flex flex-row mb-2">
                      <div class="lg:w-1/4 lg:mr-8 w-full select-none cursor-pointer bg-gray-100 rounded-lg flex flex-1 items-center p-4  transition duration-500 shadow ease-in-out transform hover:-translate-y-1 hover:shadow-lg">
                        <div class="flex-1 pl-1 mr-16">
                          <div class="font-semibold"><%= product.title %></div>
                          
                          <div class="text-gray-600 font-medium text-sm maxlength="10>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
                        </div>
                            <div class="flex-2 pl-1 mr-0">
                          <div class="font-semibold"><%= product.price %></div>
                          <%= button_to '+', line_items_path(product_id: product), class:"text-black font-medium text-sm mt-8 px-4 py-2 bg-blue rounded-full" %>
                        <div id="<%= product.id %>"></div>
                            </div>
                      </div>
                    </li>
                  </ul>
                  </div>
                  
                <% end %>
                <% end %>
                <% end %>


            </div>
                <% end %>
              <% end %>
            <% end %>
         </div>
      </div>
   </body>

你可以使用partials来实现你想要的。渲染局部时,可以传入局部变量,如下所示:

<%= render 'partial_location', local_variable: variable_name %>
<% variable_name.each do |variable| %>
  'Do something with the variable'
<% end %>

阅读此代码有一个巨大的n+1查询问题。对于您调用的每个迭代
@products.where(user_id:@account.id,category:category,available:'t')
将触发额外的数据库查询。使用关联和即时加载,不要在视图中放置SQL查询。
<% variable_name.each do |variable| %>
  'Do something with the variable'
<% end %>