Rails 3 - Ideal way to set title of pages

后端 未结 13 1233
失恋的感觉
失恋的感觉 2020-12-04 06:33

Whats the proper way to set the page title in rails 3. Currently I\'m doing the following:

app/views/layouts/application.html:


  

        
相关标签:
13条回答
  • 2020-12-04 06:45

    There's no need to create any extra function/helper. You should have a look to the documentation.

    In the application layout

    <% if content_for?(:title) %>
      <%= content_for(:title) %>
    <% else %>
      <title>Default title</title>
    <% end %>
    

    In the specific layout

    <% content_for :title do %>
      <title>Custom title</title>
    <% end %>
    
    0 讨论(0)
  • 2020-12-04 06:45

    There are already some good answers, but I'll add my simple approach. Add this to layouts/application.html

    - if content_for?(:title)
      -title = "My site | #{content_for(:title)}"
    -else
      -title = "My site | #{controller_name.titleize}"
    

    You automagically get a nice names on all your views like "My site | Posts" -- or whatever the controller happens to be.

    Of course, you can optionally set a title on a view by adding:

    - content_for(:title, 'About')
    

    and get a title like "My site | About".

    0 讨论(0)
  • 2020-12-04 06:48

    There's a simple way to manipulate layout variables (title, description, etc.):

    # 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' %>
    

    And other pages will have App default title value for their titles

    0 讨论(0)
  • 2020-12-04 06:48

    In application layout:

    # app/views/layouts/application.html.erb
    
    <title><%= (yield :title) || 'General title' %></title>
    

    then in each view where you want a specific title:

    <% content_for :title, 'Specific title' %>
    
    0 讨论(0)
  • 2020-12-04 06:50

    This is my preferred way of doing it:

    application_helper.rb

    module ApplicationHelper
      def title(*parts)
        content_for(:title) { (parts << t(:site_name)).join(' - ') } unless parts.empty?
      end
    end
    

    views/layouts/application.html.erb

    <title>
      <%= content_for?(:title) ? yield(:title) : t(:site_name) %>
    </title>
    

    config/locales/en.yml

    en:
      site_name: "My Website"
    

    This has the nice advantage to always falling back to the site name in your locales, which can be translated on a per-language basis.

    Then, on every other page (eg. on the About page) you can simply put:

    views/home/about.html.erb

    <% title 'About' %>
    

    The resulting title for that page will be:

    About - My Website

    Simples :)

    0 讨论(0)
  • 2020-12-04 06:51

    @akfalcon - I use a similar strategy, but without the helper.. I just set the default @title in the application controller and then use, <%=@title%> in my layout. If I want to override the title, I set it again in the controller action as you do. No magic involved, but it works just fine. I do the same for the meta description & keywords.

    I am actually thinking about moving it to the database so an admin could change the titles,etc without having to update the Rails code. You could create a PageTitle model with content, action, and controller. Then create a helper that finds the PageTitle for the controller/action that you are currently rendering (using controller_name and action_name variables). If no match is found, then return the default.

    @apeacox - is there a benefit of setting the title in the template? I would think it would be better to place it in the controller as the title relates directly to the action being called.

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