gpt4 book ai didi

ruby-on-rails-3.1 - active_admin - 显示属于另一个项目的项目列表

转载 作者:行者123 更新时间:2023-12-04 17:25:52 25 4
gpt4 key购买 nike

我试图以某种方式显示 line itemsorder在 active_admin order show page , 没运气..

以下是模型之间的关系:

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,我终于在ActiveAdmin中按照show_page的顺序获得了line_items
 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,但离我想要的东西不远了。干杯!

最佳答案

像这样的事情可能会奏效:

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

关于ruby-on-rails-3.1 - active_admin - 显示属于另一个项目的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com