84
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Organization

[Rails]provideヘルパでlayoutにデータを引き渡す

タイトルとかOGPとかをview側から動的にlayoutにデータを引き渡したいときの定石。

app/views/posts/index.html.erb
<% provide :title, "ページ個別タイトル" %>
<% provide :og_title, "ページ個別facebookタイトル" %>

このデータをlayoutから参照できる。

app/views/layouts/application.html.erb
<title><%= yield :title %></title>
<meta property="og:title" content="<%= yield :og_title %>" />

provide使ってないページとかあったりする場合はデフォルトの文言が表示されるようにしておくといい。

app/views/layouts/application.html.erb
<title><%= yield(:title).presence || "デフォルトタイトル" %></title>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
84
Help us understand the problem. What are the problem?