默认情况下为layouts/application.html.haml
(如果不使用haml,则为.erb
).
实际上,布局文件可以按控制器或每个操作设置,而不是按视图、按视图文件夹设置.
很少有这样的情况:
To change the default layout file for all controller (ie. use another.html.haml
instead of application.html.haml
)
class ApplicationController < ActionController::Base
layout "another"
# another way
layout :another_by_method
private
def another_by_method
if current_user.nil?
"normal_layout"
else
"member_layout"
end
end
end
将某个控制器中的所有操作更改为使用另一个布局文件
class SessionsController < ActionController::Base
layout "sessions_layout"
# similar to the case in application controller, you could assign a method instead
end
将操作更改为使用其他布局文件
def my_action
if current_user.nil?
render :layout => "normal_layout"
else
render :action => "could_like_this", :layout => "member_layout"
end
end