元内容在MVC中存在于何处?

我一直在考虑MVC中的元内容,特别是页面标题和元描述(这对于指导Google在搜索结果中显示的代码段非常有用)。

我不能确定这应该在哪里生活。 根据(对于UGC应用程序)读者与内容的互动方式,通常会有一些逻辑。

我无法确定在视图层或控制器中是否更好地构造了此元内容。 它几乎肯定不会存在于模型中,因为它特定于数据的特定视图,但是当我的第一直觉是将它放在视图中时我觉得它可能更好地被抽象化。

我对其他人采取的方法很感兴趣。

元内容通常使用helper, content_foryield

例如:

 # app/helpers/application_helper.rb def title(title) content_for :title, title end def description(description) content_for :description, description end # app/views/layouts/application.html.erb My app <%= yield :title %> <%= yield :description %> # app/views/some_controller/some_action.html.erb <% title @an_instance.an_attribute # or whatever you want by the way description @an_instance.another_attribute %> 

如果您打算进行流式传输 ,则应在助手中使用provide而不是content_for

永远不要在控制器中放置用于元内容的实例变量(例如@title = 'blabla'; @description = 'blablabla'

以下是一些相同的资源(列出非详尽的):

  • 建立防弹视图 (幻灯片可下载)
  • Rails最佳实践 (商业)