デフォルトでは全てのviewファイルがapplication.html.erb
をレイアウトとして利用するようになっています。
コントローラーやアクションごとに、使用するレイアウトを指定する方法を調べました。
レイアウトファイル側から指定
レイアウトファイルの名前をviews/layouts/hoges.html.erb
にすることで、hoges_controller
内の全てのアクションに適用されます。
コントローラー側から指定
コントローラーの内上部にlayout '<layoutファイル名の冒頭>'
と書き込むことで、そのコントローラー内のアクションで利用するレイアウトファイルを指定することができます。
hoges_controller.rb
class HogesController < ApplicationController
layout 'another'
def index
end
.
.
(レイアウトファイル名がanother.html.erb
の場合)
アクションごとに指定
アクション内にrender layout: '<'layoutファイル名の冒頭'>
と書き込むことで、そのアクションで利用するレイアウトファイルを指定することができます。
hoges_controller.rb
class HogesController < ApplicationController
layout 'another'
def index
render layout: 'another'
end
.
.
(レイアウトファイル名がanother.html.erb
の場合)