Pet*_*xey 6 model-view-controller ruby-on-rails
我一直在考虑MVC中的元内容,特别是页面标题和元描述(这对于指导Google在搜索结果中显示的代码段非常有用).
我不能确定这应该在哪里生活.根据(对于UGC应用程序)读者与内容的交互方式,通常会有一些逻辑.
我无法确定是否在视图层或控制器中更好地构造了此元内容.它几乎肯定不会存在于模型中,因为它特定于数据的特定视图,但是当我的第一直觉是将它放在视图中时,我觉得它可能更好地被抽象化.
我对其他人采取的方法很感兴趣.
元内容通常使用帮助程序设置,content_for并且yield.
例如:
# 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
<title>My app <%= yield :title %></title>
<meta name="description"><%= yield :description %></meta>
# 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
%>
Run Code Online (Sandbox Code Playgroud)
如果您打算进行流式传输,则应该使用provide而不是content_for在助手中.
永远不要在控制器中放置用于元内容的实例变量(例如@title = 'blabla'; @description = 'blablabla')
以下是一些相同的资源(列出非详尽的):
| 归档时间: |
|
| 查看次数: |
914 次 |
| 最近记录: |