我有一個與發佈者has_many關係的發佈模型。在模型中,我有一個的意思創建一個方法的HTML,準備好行:Rails:在模型中創建HTML
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是不好的做法,因爲生成表示是幫助者和意見的責任 –
這很好理解。我在最後一天一直在研究助手,他們似乎在鐵軌方面聲譽不盡相同。像[this]這樣的文章(http://nicksda.apotomo.de/2011/10/rails-misapprehensions-helpers-are-shit/)和[this](http://blog.steveklabnik.com/posts/2011- 09-09-better-ruby-presenters)不要將它們投射在最佳光線下。但是,他們的擔心可能被誇大了? – nullnullnull