In Rails, if yield is a closure inside a layout, then how does controller can dictate which layout to render?
layout :render => "myLayout"
In Rails, if yield is a closure inside a layout, then how does controller can dictate which layout to render?
layout :render => "myLayout"
控制器中的layout
命令也可以接受一个符号,它将它链接到一个方法。像这样:
class ApplicationController < ActionController::Base
layout :layout_by_resource
protected
def layout_by_resource
if devise_controller? && resource_name == :admin_user
"devise_admin"
else
"application"
end
end
end
因此,您可以使用它从控制器中切换出要使用的布局。
如果要为控制器设置布局
class YourController < ApplicationController
before_filter :set_layout
protected
def set_layout
render :layout => 'Your-layout'
end
end