1

I´m working on a E-commerce app. The store manager is able to upload products via active admin. When products are uploaded it is assigned to a category and a label in the active admin panel, that works very well with out problems.

The most recent product in each category is displayed as the category front image on the views/pages/index.html.erb via this code below and it works like a charm.

   views/pages/index.html.erb

   <% @products.each_slice(3) do |products_group| %>
    <div class="row">
      <% products_group.each do |category, products| %>

            <% products.each_with_index do |product, index| %>
                <% if index == 0 %>
                    <div class="col-lg-4 col-sm-6 col-xs-12 center-block " >

                    <%= link_to category_path (category), { :method => 'GET' } do %>
                        <%= image_tag product.image.url(:medium), class: "img-responsive" %>
                    <% end %>
            <div class="caption">
                <p class="category-name" ><%= product.category.name %></p>
             </div> 
            <% end %>
            <% end %>
            </div> 
        <% end %>
        </div>
    <% end %>

When the customer clicks on of the image links in the views/pages/index.html.erb the customer is taken to the category page pages/categories/show.html.erb there the customer can browse through all products in the selected category.

pages/categories/show.html.erb

   <div class="container-fluid">
     <div class="row category_top"> 
       <% @products.each do |product| %>
        <div class="col-lg-3 col-sm-6 col-xs-12 center-block " >
            <%= link_to product_path (product) do %>
            <%= image_tag product.image.url(:medium), class: "img-responsive" %>
        <% end %>

       <div class="product_description">
         <h5><%= link_to product.title, product %></h5>                  
         <p><%= social_share_button_tag(product.title) %></p>
       </div>
       </div>
      <% end %>
   </div>
  </div>

The problem is that on the pages/categories/show.html.erb the store manager would like some products to appear next to each other.

For example: 4days ago he uploaded a product (productA) and today he uploaded a product(productB) that he wants to be displayed next to productA. But in between those products are 100´s of other products.

So the context of my problem is: How is the Store manager able to re-arrange the products in the active admin panel so productA and productB sits next to each other in the pages/categories/show.html.erb view? I that even possible?

p.s. this is the first time I've been working with active admin

Here is the app/admin/admin_user.rb

  ActiveAdmin.register AdminUser do
    permit_params :email, :password, :password_confirmation

  index do
    selectable_column
    id_column
    column :email
    column :current_sign_in_at
    column :sign_in_count
    column :created_at
    actions
  end

  filter :email
  filter :current_sign_in_at
  filter :sign_in_count
  filter :created_at

  form do |f|
    f.inputs "Admin Details" do
      f.input :email
      f.input :password
      f.input :password_confirmation
    end
    f.actions
  end

end

here is the categories_controller.rb

    class CategoriesController < ApplicationController
     before_action :set_category, only: [:show, :edit, :update, :destroy]
  def index
    @categories = Category.all
  end

   def show
    @products = @category.products
     @images  = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]
     @random_no = rand(5)
     @random_image = @images[@random_no]
  end

 private
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.includes(:products).find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name, :slug)
    end


end
DaudiHell
  • 808
  • 10
  • 32

1 Answers1

1

I have not used it myself but perhaps Active Admin Sortable Table is what you are looking for.

Piers C
  • 2,880
  • 1
  • 23
  • 29