Rails 3 – 设置页面标题的理想方式
什么是在rails中设置页面标题的正确方法3.目前我正在做以下几点:
应用程序/视图/布局/ application.html:
<head> <title><%= render_title %></title> <%= csrf_meta_tag %> 应用程序/佣工/ application_helper.rb:
 def render_title return @title if defined?(@title) "Generic Page Title" end 
应用程序/控制器/ some_controller.rb:
 def show @title = "some custom page title" end 
有没有更好的方法来做到这一点?
你可以简单的帮手:
 def title(page_title) content_for :title, page_title.to_s end 
在你的布局中使用它:
 <title><%= yield(:title) %></title> 
然后从你的模板中调用它:
 <% title "Your custom title" %> 
希望这可以帮助 ;)
没有必要创build任何额外的function/帮手。 你应该看看文档 。
在应用程序布局
 <% if content_for?(:title) %> <%= content_for(:title) %> <% else %> <title>Default title</title> <% end %> 
在具体的布局
 <% content_for :title do %> <title>Custom title</title> <% end %> 
我发现apeacox的解决scheme不适合我(在Rails 3.0.3中)。
相反,我做了…
 在application_helper.rb : 
 def title(page_title, options={}) content_for(:title, page_title.to_s) return content_tag(:h1, page_title, options) end 
在布局中:
 <title><%= content_for(:title) %></title> 
在看法:
 <% title "Page Title Only" %> 
要么:
 <%= title "Page Title and Heading Too" %> 
请注意,这也允许我们检查是否存在标题,并在视图未指定标题的情况下设置默认标题。
在布局中,我们可以做一些事情:
 <title><%= content_for?(:title) ? content_for(:title) : 'This is a default title' %></title> 
这是我喜欢的方式:
application_helper.rb
 module ApplicationHelper def title(*parts) content_for(:title) { (parts << t(:site_name)).join(' - ') } unless parts.empty? end end 
视图/布局/ application.html.erb
 <title> <%= content_for?(:title) ? yield(:title) : t(:site_name) %> </title> 
configuration/语言环境/ en.yml
 en: site_name: "My Website" 
这有一个很好的优势,总是回落到您的语言环境中的站点名称,这可以在每个语言的基础上进行翻译。
然后,在其他页面上(例如关于页面),您可以简单地放置:
意见/家/ about.html.erb
 <% title 'About' %> 
该页面的结果标题将为:
关于 – 我的网站
简单:)
@akfalcon – 我使用类似的策略,但没有帮手..我只是在应用程序控制器中设置默认的@title,然后在我的布局中使用<%= @ title%>。 如果我想重写标题,我将在控制器操作中再次设置它。 没有魔法介入,但它工作得很好。 我对元描述和关键字也是一样的。
我实际上正考虑将其移动到数据库,以便pipe理员可以更改标题等,而无需更新Rails代码。 您可以创build一个包含内容,操作和控制器的PageTitle模型。 然后创build一个帮助程序,用于查找当前正在呈现的控制器/操作的PageTitle(使用controller_name和action_namevariables)。 如果找不到匹配,则返回默认值。
@apeacox – 是否有在模板中设置标题的好处? 我认为把它放在控制器中会更好,因为标题直接与所调用的动作有关。
我更喜欢这个:
 module ApplicationHelper def title(*page_title) if Array(page_title).size.zero? content_for?(:title) ? content_for(:title) : t(:site_name) else content_for :title, (Array(page_title) << t(:site_name)).join(' - ') end end end 
 如果title没有参数被调用,它会返回标题的当前值,或者在这个例子中的默认值是“Example”。 
 它的title被调用参数,它将其设置为传递的值。 
 # layouts/application.html.erb <title><%= title %></title> # views/index.html.erb <% title("Home") %> # config/locales/en.yml en: site_name: "Example" 
你也可以检查这个railscast 。 我认为这将是非常有用的,并给你基本的开始。
注意:如果你想用pjax更多的dynamic页面
 我有一个更复杂的解决scheme。 我想在我的语言环境文件中pipe理所有的标题。 我也想要包含有意义的标题来显示和编辑页面,以便资源的名称包含在页面标题中。 最后,我想在每个页面标题中包含应用程序名称,例如Editing user Gustav - MyApp 。 
 为了完成这个任务,我在application_helper.rb创build了一个帮助application_helper.rb ,它完成了大部分繁重的工作。 这会尝试从区域设置文件中获取给定操作的名称,如果存在,则会为分配的资源指定一个名称,并将这些名称与应用程序名称结合使用。 
 # Attempt to build the best possible page title. # If there is an action specific key, use that (eg users.index). # If there is a name for the object, use that (in show and edit views). # Worst case, just use the app name def page_title app_name = t :app_name action = t("titles.#{controller_name}.#{action_name}", default: '') action += " #{object_name}" if object_name.present? action += " - " if action.present? "#{action} #{app_name}" end # attempt to get a usable name from the assigned resource # will only work on pages with singular resources (show, edit etc) def object_name assigns[controller_name.singularize].name rescue nil end 
您将需要在您的语言环境文件中添加以下forms的特定于操作的文本:
 # en.yml titles: users: index: 'Users' edit: 'Editing' 
如果你想在你的单一视图中使用有意义的资源名称,你可能需要添加一些代理方法,例如
 # User.rb def name username end 
我认为这将是好事:
 <title> <% if @title %> <%= @title %> <% else %> Your title <% end %> </title> 
 并在你的控制器给@title一个值,或标题将是Your title 
我的答案更简单:
locales / any_archive.yml :
 pt-BR: delivery_contents: title: 'Conteúdos de Entregas' groups: title: 'Grupos' 
而在application.html.slim里面:
 title = "App Name: #{t("#{controller_name.underscore}.title")}" 
有一个简单的方法来操作布局variables(标题,描述等):
 # app/views/application.html.erb <title> <%= content_for :title || 'App default title' %> </title> # app/views/posts/index.html.erb <%= content_for :title, 'List of posts' %> 
 而其他页面将为其标题App default title值 
在应用布局中:
 # app/views/layouts/application.html.erb <title><%= (yield :title) || 'General title' %></title> 
然后在每个你想要特定标题的视图中:
 <% content_for :title, 'Specific title' %> 
已经有一些很好的答案,但是我会添加我的简单方法。 将此添加到layouts / application.html
 - if content_for?(:title) -title = "My site | #{content_for(:title)}" -else -title = "My site | #{controller_name.titleize}" 
您可以自动获取所有视图上的好名字,例如“我的网站|post” – 或控制器碰巧是什么。
当然,您可以通过添加以下选项来在视图上select设置标题:
 - content_for(:title, 'About') 
并获得像“我的网站|关于”的标题。