我使用Rails作爲REST服務器,並且我的一個模型有一個描述字段。在該模型的JSON表示中,我需要輸出常規描述字段和稱爲description_markdown的任意非數據庫屬性,該屬性就是通過降落過濾器運行的描述。我已經知道如何用隆重的接待,像運行通過降價文本:在Rails模型中包含任意字段JSON輸出
@post = Post.find(params[:id])
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true, :lax_html_blocks => true)
description_markdown = markdown.render(@post.description).html_safe
但我真正需要的是讓description_markdown
上@post屬性(如@ post.description_markdown),並使其在輸出@後的JSON表示。