2012-04-01 114 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

相关问题