Home Display products "if" part of category
Reply: 1

Display products "if" part of category

Jake
1#
Jake Published in 2018-01-12 19:26:10Z

I have a product page with 4 different category tabs on it. A created product can only belong to one of the 4 categories/tabs on the page. These categories are all displayed on the top of the tab-pane via images side-by-side. What needs to be added to the <% @packs.each do |pack| %> section to load the products "if" they are of that category, thank you!

This is an example of one of the tab Category selection links...

<a href="#samplePacks" aria-controls="samplePacks" data-toggle="tab">
  <h2 class="base-text">Samples</h2>
  <%= image_tag("btn_Category_1.png", :alt => "Category one", class: "image-pad center") %>
</a>

Followed by its corresponding tab, linked to the selector above...

<div role="tabpanel" class="tab-pane fade in active" id="samplePacks">
  <div class="wellone pack-well">
    <div class="row" id="samplePillars">

      <% @packs.each do |pack| %>
        <div class="col-md-4 pack-pad">
          <%= link_to pack_path(pack) do %>
            <%= image_tag("#{pack.art_link}", :alt => "Product Image", :width => 333, :height => 333, class: "feat-img") %>
          <% end %>
        </div>
      <% end %>

    </div>
  </div>
</div>
Josh Brody
2#
Josh Brody Reply to 2018-01-12 19:30:24Z

You can do it a few different ways. Here's two:

In your controller, @packs = @packs.group_by { |pack| pack.category.name } should result in something like {'samplePillars' => [Pack, Pack]}

In your view

@packs.select { |pack| pack.category.name == 'samplePillars' }.each do ... end will return an array of @packs where the category.name is samplePillars

There are more idiomatic ways of doing this but for the sake of getting started, these two are fine.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328532 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO