2012-04-01 32 views
5

Tôi cố gắng để hiển thị bằng cách nào đó line items cho order trong active_admin order show page, không may mắn ..active_admin - hiển thị một danh sách các mục thuộc về mục khác

đây là những mối quan hệ giữa các mô hình:

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 

khi tôi nhấp vào hiển thị theo thứ tự, nó phải hiển thị các mục cho đơn đặt hàng này .. Trong ứng dụng của hiển thị tệp tôi đã làm với

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

và các mặt hàng đang ở trang, nhưng trong Active_Admin Tôi không biết làm thế nào để làm cho nó làm việc .. Xin vui lòng giúp đỡ. Cảm ơn bạn đã dành thời gian.

Giải Quyết

Nhờ bruno077 tôi quản lý để cuối cùng nhận được line_items trong show_page trật tự trong 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 

tôi ID của sản phẩm cho bây giờ, nhưng đó là cách đây không xa để có được những gì tôi muốn. Chúc mừng!

Trả lời

10

Something như thế này có thể làm việc:

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 

Một ví dụ khác liên quan mà có thể cung cấp cho bạn một gợi ý:

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

lỗi cú pháp, bất ngờ '{', mong kEND cột 'tiêu đề' {product .title} có 4 lỗi thuộc loại này, 1 lỗi cho mỗi "{" bạn có thể xem nó không? Cảm ơn bạn rất nhiều vì đã cố gắng. – rmagnum2002

+0

nó tạo ra các cột, fot điều này chỉ có tôi bỏ phiếu +1, điều này là reallly hữu ích, nhưng tôi thực sự cần phải nhận được mối quan hệ này để làm việc trong Active Admin. – rmagnum2002

+0

Tôi nghĩ rằng có thể {} ký hiệu có thể không hoạt động với ActiveAdmin, tôi sẽ cập nhật câu trả lời. – bruno077

Các vấn đề liên quan