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 %>×</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!
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With