Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active_admin - display a list of items that belongs to another item

I am trying to display somehow the line items for the order in the active_admin order show page, no luck..

here are the relations between models:

order.rb

class Order < ActiveRecord::Base
  has_many :line_items, :dependent => :destroy
  # ...
  validates :name, :address, :email, :presence => true
  validates :pay_type, :inclusion => PAYMENT_TYPES
end

line_item.rb

class LineItem < ActiveRecord::Base
  belongs_to :order
  belongs_to :product
  belongs_to :cart

  def total_price
    product.price * quantity
  end

end

active_admin order.rb

ActiveAdmin.register Order do

  show do
    attributes_table :name, :email, :address, :pay_type, :created_at, :updated_at
  end
end

active_admin line_item.rb

class LineItem < ActiveRecord::Base
  belongs_to :order
  belongs_to :product
  belongs_to :cart

  def total_price
    product.price * quantity
  end

end

when I click show order, it must display the items for this order.. In application's show file I did it with

<%= render @order.line_items %>

_line_items.html.erb

<!-- START_HIGHLIGHT -->
<% if line_item == @current_item %>
    <tr id="current_item">
  <% else %>
<tr>
<% end %>
<!-- END_HIGHLIGHT -->
  <td><%= line_item.quantity %>&times;</td>
  <td><%= line_item.product.title %></td>
  <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>

and the items are in the page, but in Active_Admin I don't know how to make it work.. Please help. Thank you for your time.

Solved

Thanks to bruno077 I managed to finally get the line_items in the order show_page in ActiveAdmin

 show do |order|

  panel "Customer details" do
    attributes_table_for order, :first_name, :last_name, :card_type, :created_at, :ip_address
  end

  panel("Products for this order") do
    table_for(order.line_items) do
      column "Product" do |item| 
        item.product.title
      end
      column "Price" do |item| 
        item.product.price
      end
      column "Quantity" do |item| 
        item.quantity
      end
    end
  end
end

I got the ID of the product for now, but it's not far from here to get what I want. Cheers!

like image 297
rmagnum2002 Avatar asked Apr 01 '12 04:04

rmagnum2002


People also ask

How to display a filter for an attribute in active admin?

To display a filter for an attribute, use the filter method Out of the box, Active Admin supports the following filter types: :string - A drop down for selecting “Contains”, “Equals”, “Starts with”, “Ends with” and an input for a value. :numeric - A drop down for selecting “Equal To”, “Greater Than” or “Less Than” and an input for a value.

How do I search for a specific label in active admin?

By default, Active Admin will try to use ActiveModel I18n to determine the label. You can also filter on more than one attribute of a model using the Ransack search predicate syntax . If using a custom search method, you will also need to specify the field type using :as and the label.

Can I customize the menu for the namespace in active admin?

In this case, you can customize the menu for the namespace within the Active Admin initializer. This will be registered on application start before your resources are loaded. If your administrators have different access levels, you may sometimes want to scope what they have access to.

What types of filters does active admin support?

Out of the box, Active Admin supports the following filter types: :string - A drop down for selecting “Contains”, “Equals”, “Starts with”, “Ends with” and an input for a value. :numeric - A drop down for selecting “Equal To”, “Greater Than” or “Less Than” and an input for a value.


1 Answers

Something like this might work:

ActiveAdmin.register Order do
  show do |order|
    div do      
      panel("Items") do
        table_for(order.line_items) do
          column :quantity
          column "Title" do |i| 
            i.product.title
          end
          column "Price" do |i| 
            number_to_current(i.total_price)
          end
        end
      end
    end
  end
end

Another unrelated example which might give you a hint:

  # => Show
  show :title => :date do |gallery|
    panel "Galería" do
      attributes_table_for gallery, :name, :description, :date
    end

    panel "Fotos" do
      table_for(gallery.gallery_files) do
        column "Título", :title
        column "Fecha", :date
        column "Foto" do |image|
          image_tag image.file.url(:thumb).to_s
        end
      end
    end

  end
like image 107
bruno077 Avatar answered Nov 15 '22 12:11

bruno077