【发布时间】:2012-11-19 18:39:09
【问题描述】:
我有一个与贡献者有 has_many 关系的发布模型。在模型中,我有一个方法可以按行创建 html-ready:
def authors
authors = []
Contributor.where(:publication_id => self.id).each do |author|
authors << "link_to "+author.name+", Contributor.find("+author.id.to_s+")"
end
authors.to_sentence
end
在我看来,我有以下几行:
by <%= @publication.authors %>
但不是渲染链接,而是渲染原始代码,比如:
by link_to B, Contributor.find(1)
我尝试通过将 .html_safe 添加到 @publication.authors 的末尾来修补此问题,但无济于事。有没有更好的方法将这些链接从模型传输到视图?
【问题讨论】:
-
如果您在“模型”中创建响应,那么它不是 MVC。它只是带有 ORM 的经典意大利面条代码。
-
在“模型”层生成 HTML 是不好的做法,因为生成表示是助手和视图的责任
标签: html ruby-on-rails ruby-on-rails-3