Rails: How to change the title of a page?

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

    You can also set it in a before_filter in your controller.

    # foo_controller.rb
    
    class FooController < ApplicationController
    
      before_filter :set_title
    
      private
    
      def set_title
        @page_title = "Foo Page"
      end
    
    end
    
    # application.html.erb
    
    

    <%= page_title %>

    You can then set conditions in the set_title method to set a different titles for different actions in the controller. It's nice to be able to see all the relevant page titles within your controller.

提交回复
热议问题