2012-04-01 119 views
5

我試圖以某種方式顯示line items在active_admin order show page,沒有運氣的order ..active_admin - 顯示屬於另一個項目的項目列表

這裏是模型之間的關係:

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 

當我點擊顯示訂單,它必須顯示此訂單的項目。在應用程序的顯示文件我用

<%= 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> 

和項目在頁面上,但在Active_Admin我不知道如何使它工作..請幫助。感謝您的時間。

解決

感謝bruno077我設法最終得到的順序show_page的line_items在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 

我的產品的ID現在,但它離這裏並不遠得到我想要的。乾杯!

回答

10

像這樣的東西可能會奏效:

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 

這可能會給你一個提示的另一個不相關的例子:

# => 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 
+0

語法錯誤,意外 '{',希望DMOZ目錄 列 '標題'{產品.title}這類錯誤有4個,每個「{」可以請你看一下嗎?至少非常感謝你的嘗試。 – rmagnum2002 2012-04-02 22:15:20

+0

它創建的列,FOT這件事只有我投+1,這是reallly有益的,但我真的需要得到這個關係中主動管理工作。 – rmagnum2002 2012-04-02 22:26:13

+0

我認爲{}符號可能不適用於ActiveAdmin,我會更新答案。 – bruno077 2012-04-02 23:02:21

相關問題