Rails: How to change the title of a page?

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

提交回复
热议问题