將模型中的link_to內部資源添加到模型中我有要求在DataTables報告中包含內部鏈接。因此,我必須從Model#as_json返回報告數據,例如:如何使用rails 2.3.x
class Error < ActiveRecord::Base
include ActionView::Helpers::UrlHelper # provides link_to
include ActionController::UrlWriter # provides *_path
def as_json(options={})
{
:date => self.created_at,
:level => self.level,
:ip => self.ip,
:title => truncate(self.title, :length => 100),
:show => link_to('Show', error_path(self)),
:hide => self.handled ? "" : "#{link_to 'Hide', handle_error_path(self)}"
}
end
...
什麼是我想要包括什麼努力。但現在我得到錯誤:「無法將字符串轉換爲哈希」
這是因爲'link_to'使用'url_for'這是UrlHelper和UrlWriter都具有實際行爲不同的方法。
所以我在我的智慧結束。如果有人能幫我弄清楚如何做到這一點,或者告訴我如何在不打破MVC的情況下滿足要求,我將非常感激任何方式。