Rails Controller で layout
を複数呼び出すと後勝ちで上書きされる
例
some_controller.rb
class SomeController < ApplicationController
layout 'layout_for_index', only: :index
layout 'layout_for_show', only: :show
def index; end
def show; end
end
結果
-
some#index
-> デフォルトの layout が適用される -
some#show
->layout_for_show
が適用される
layout
にはメソッド名やprocを渡せるのでこんな感じで書けばいける
some_controller.rb
layout :determine_layout
...
def determine_layout
case action_name
when 'index' then 'layout_for_index'
when 'show' then 'layout_for_show'
end
end
該当コード見ると普通に代入してるので当然の動きですね
rails/actionview/lib/action_view/layouts.rb
def layout(layout, conditions = {})
include LayoutConditions unless conditions.empty?
conditions.each { |k, v| conditions[k] = Array(v).map(&:to_s) }
self._layout_conditions = conditions
self._layout = layout
_write_layout_method
end