Rails: How to change the title of a page?

前端 未结 15 1325
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 17:08

What is the best way to create a custom title for pages in a Rails app without using a plug-in?

15条回答
  •  情话喂你
    2020-11-30 17:28

    In your views do something like this:

    <% content_for :title, "Title for specific page" %>
    
    

    <%= content_for(:title, "Title for specific page") %>

    The following goes in the layout file:

    
      <%= yield(:title) %>
      
    
    
      
      

    <%= yield(:title) %>

    It's also possible to encapsulate the content_for and yield(:title) statements in helper methods (as others have already suggested). However, in simple cases such as this one I like to put the necessary code directly into the specific views without custom helpers.

提交回复
热议问题