[Ruby on Rails] ページごとにtitleを変更する方法
- 2013 1/10
- カテゴリー : Ruby on Rails
- コメントを書く
Ruby on Rails でページごとにtitleを変更する方法をメモ。
■ ERB
<title><%= content_for?(:title) ? yield(:title) : "default title"%></title> |
■ Haml
%title>< - if content_for? :title = yield :title |デフォルトタイトル - else デフォルトタイトル |
metaタグのdescriptionやkeywordsも同じように書けます。
■ app/views/layouts/application.html.haml
- if content_for? :meta_description - meta_description = yield :meta_description - else - meta_description = 'default description' %meta(content="#{meta_description}" name="description") - if content_for? :meta_keywords - meta_keywords = yield :meta_keywords - else - meta_keywords = 'default keywords' %meta(content="#{meta_keywords}" name="keywords") |
■ どこかの view
= content_for :meta_description do #{@item.description} = content_for :meta_keywords do #{@item.name} = content_for :title do #{@item.name} |