我的Ticket
模型应用程序中有一些相当复杂的 JSON 响应,我希望我TicketDecorator
能成为构建这些响应的人。
但是,我已经为我的系统设置了 API,并使用 RABL 来构建这些 JSON 响应,所以我想重用我已经创建的模板。
我想知道是否可以从 inside 的方法中呈现 RABL 模板TicketDecorator
,如下所示:
这是我的 RABL 模板tickets/show.json.rabl
object @ticket
attributes :id, :link, :reported_ago_in_words_with_reporter,
:last_updated_in_words_with_updater, :priority_label, :status,
:location, :category_list
node(:attachment_urls) { |ticket| [ asset_path(ticket.first_attachment_url), asset_path(ticket.second_attachment_url), asset_path(ticket.third_attachment_url) ] }
node(:comment_count) { |ticket| ticket.comments.count }
child :recent_comments do
extends 'comments/index'
end
这是我的TicketDecorator
方法:
class TicketDecorator < ApplicationDecorator
def as_json
h.render(template: "tickets/show", formats: :json)
end
end
但是,这不起作用,因为我无法成功设置@ticket,并且我收到一条错误消息: undefined method `first_attachment_url' for nil:NilClass
因为@ticket
是 nil。
关于如何使这两者很好地协同工作,有人有什么好的解决方案吗?
我唯一真正的想法是将模板渲染为字符串并手动使用 RABL,但我不确定如何render_to_string
在 Draper 内部调用,因为它不是视图助手。
对此有什么想法吗?