LoginSignup
98
82

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-24

タイトルとか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>
98
82
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
98
82