在我的一个控制器中,我想在某些条件下更改布局,否则保留父 ApplicationController 使用的默认布局(最初是“应用程序”,但我现在正在尝试其他一些布局)。尝试使用 alias_method 访问“布局”,但它似乎不起作用。我的代码:
class SomeController < ApplicationController
alias_method :parent_layout, :layout
layout :some_layout
def some_layout
if some_condition
"new_layout"
else
:parent_layout
end
end
end
这给出了一个错误:
ActionController::RoutingError (undefined method `layout' for class `SomeController'):
app/controllers/some_controller.rb:6:in `alias_method'
app/controllers/some_controller.rb:6:in `<class:SomeController>'
app/controllers/some_controller.rb:3:in `<top (required)>'