active_admin - 显示属于另一个项目的项目列表

时间:2012-04-01 04:14:20

标签: ruby-on-rails-3.1 activeadmin

我试图以某种方式显示active_admin line itemsorder的{​​{1}},没有运气..

以下是模型之间的关系:

order.rb

order show page

line_item.rb

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

active_admin order.rb

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

  def total_price
    product.price * quantity
  end

end

active_admin line_item.rb

ActiveAdmin.register Order do

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

当我点击显示顺序时,它必须显示此订单的项目..在应用程序的show文件中我用

完成了
class LineItem < ActiveRecord::Base
  belongs_to :order
  belongs_to :product
  belongs_to :cart

  def total_price
    product.price * quantity
  end

end

_line_items.html.erb

<%= render @order.line_items %>

并且项目在页面中,但在Active_Admin中我不知道如何使其工作..请帮忙。谢谢你的时间。

解决

感谢bruno077,我终于在ActiveAdmin的show_page中获得了line_items

<!-- 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>

我现在得到了该产品的ID,但距离这里不远就能得到我想要的东西。干杯!

1 个答案:

答案 0 :(得分: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