Rails 3 - Ideal way to set title of pages

后端 未结 13 1290
失恋的感觉
失恋的感觉 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 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".

提交回复
热议问题