Rails: How to change the title of a page?

前端 未结 15 1224
没有蜡笔的小新
没有蜡笔的小新 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:30

    An improvement on @opsb and a more complete form of @FouZ's:

    In application.html.erb:

    <title><%= @title || "Default Page Title" %></title>
    

    In the view erb file or its controller:

    <% @title = "Unique Page Title" %>
    
    0 讨论(0)
  • 2020-11-30 17:34

    Without further details on the use-case or requirements that you're trying to satisfy, I can think of several alternatives:

    1) Switch the title in one of your layout pages and consume a helper method stored in application_helper.rb

    <title><%= custom_title %></title>
    

    This approach will give you a unique title for each layout page.

    2) Railscasts suggests using a partial to load what shows up between the HEAD tags

    3) Use javascript/ajax calls to manipulate the DOM if you need to change the title after the load event.

    Maybe you don't really want to change the content tagged by the title element. Perhaps you really need a breadcrumb of some sort, so that your users always know where they are with respect to your site's navigation hierarchy. While I've done fine with how the goldberg plugin, I'm sure there are other ways of pulling off the same functionality.

    0 讨论(0)
  • 2020-11-30 17:35

    I like opsb's method, but this method works too.

    <% provide(:title,"ttttttttttttttttttZ") %>
    <html>
      <head><title><%= yield(:title) %></title></head>
       <body></body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 17:39

    I use this plugin these Rails helpers I wrote: http://web.archive.org/web/20130117090719/http://henrik.nyh.se/2007/11/my-rails-title-helpers/

    0 讨论(0)
  • 2020-11-30 17:39

    approach for page titling using content_for method and a partial

    1 . partial name : _page_title.html.erb

    <%content_for :page_title do %>
     <%=title%>
    <%end%>
    

    2 . Usage in application.html.erb inside title tag

       <title><%=yield :page_title %></title>
    

    3 . Usage in respective *.html.erb excluding application.html.erb Just stick this in any .html.erb and supply title of the page

        e.g : inside index.html.erb
    
        <%=render '_page_title', title: 'title_of_page'%>
    
    0 讨论(0)
  • 2020-11-30 17:41

    I would like to add my pretty simple variant.

    In the ApplicationController define this method:

      def get_title
        @action_title_name || case controller_name
                                when 'djs'
                                  'Djs'
                                when 'photos'
                                  'Photos'
                                when 'events'
                                  'Various events'
                                when 'static'
                                  'Info'
                                when 'club'
                                  'My club'
                                when 'news'
                                  'News'
                                when 'welcome'
                                  'Welcome!'
                                else
                                  'Other'
                              end
      end
    

    After that you can call get_title from your layout's title tag. You can define more specific title for your page by defining @action_title_name variable in your actions.

    0 讨论(0)
提交回复
热议问题