RailsTutorial4章
application_helperの使い方
メソッドが呼ばれるとき→viewで
apprication_helper.rb
module ApplicationHelper # ページごとの完全なタイトルを返します。 def full_title(yield_title) base_title = "Ruby on Rails Tutorial Sample App" if yield_title.empty? base_title else yield_title + " | " + base_title end end end
application.html.erb
<!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <%= yield %> </body> </html>
home.html.erb
<% provide(:title, "Home") %> <!DOCTYPE html> <html> <head> <title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title> </head> <body> <h1>Sample App</h1> <p> This is the home page for the <a href="https://railstutorial.jp/">Ruby on Rails Tutorial</a> sample application. </p> </body> </html>
参考 https://qiita.com/yukiyoshimura/items/f0763e187008aca46fb4
- full_title(yield(:title))ここで引数を渡している。
<% provide(:title, "Help") %>
<title><%= yield(:title) %>
provideで定義してyieldで呼び出す。