Rails: How to change the title of a page?

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

    <%= custom_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.

提交回复
热议问题