我有一个网站,客户可以在该网站上购买活动的门票,并创建了Reporter :: EventTickets类来为所需的相关信息生成哈希。这个Reporter :: EventTickets类在Reports控制器中被调用,最终我想在管理报告页面上查看它。
问题我无法在haml视图文件中将信息显示在我的桌子上!我已经尽我所能尝试了一切,却完全不知所措。
注意事项信息已成功保存到数据库(PostgreSQL),并且EventTickets类正在正确转换数据(通过RSpec测试)。
代码
class Reporter::EventTickets
include Virtus.model
def events
products.map do |product|
line_items = LineItem.includes(:order)
.where("orders.completed_at IS NOT NULL")
.where(purchasable_type: "Product", purchasable_id: product.id)
.references(:order)
ticket_purchases = line_items.map do |line_item|
order = line_item.order
[order.bill_address_full_name, order.email, line_item.quantity]
end
total = ticket_purchases.reduce(0) { |sum, purchase| sum + purchase.last }
{
date: product.event_date,
name: product.name,
total: total,
purchases: ticket_purchases.sort
}
end
end
private
def products
Ticket.where("event_date >= ?", week_ago_in_time_zone).order("event_date ASC")
end
def week_ago_in_time_zone
(Time.current - 7.days).to_date
end
end
控制器
def event_tickets
@reporter = Reporter::EventTickets.new
@csv_link = admin_reports_event_tickets_path(format: :csv)
respond_with @reporter
end
模型
class Order < ActiveRecord::Base
has_many :line_items, dependent: :destroy
class LineItem < ActiveRecord::Base
belongs_to :order, touch: true
belongs_to :purchasable, polymorphic: true
class Product < ActiveRecord::Base
include Purchasable
has_many :line_items, as: :purchasable
查看
= render partial: "admin/shared/header", locals: {title: "Event Ticket Purchases"}
.container-fluid.padded
= render partial: "admin/shared/notifications"
.row-fluid.hidden-print
= link_to "Download CSV", @csv_link, class: "btn btn-blue"
.row-fluid
- @reporter.events.each do |event|
%h3
= event[:date].strftime("%_m/%e")
= event[:name]
.box
%table.table.table-normal
%thead
%tr
%td Name
%td Email
%td Tickets
%tbody
- event[:purchases].each do |purchase|
%tr
- purchase.each do |column|
%td= column
%tr
%td{:colspan => "2"}
%b TOTAL
%td
%b= event[:total]
Rails不会出错,并且页面会加载标题和按钮。该表只是不填充。