每个动作的 Rails 布局? [英] Rails layouts per action?
问题描述
我对某些操作使用了不同的布局(主要用于大多数控制器中的新操作).
I use a different layout for some actions (mostly for the new action in most of the controllers).
我想知道指定布局的最佳方式是什么?(我在同一个控制器中使用了 3 个或更多不同的布局)
I am wondering what the best way to specify the layout would be? (I am using 3 or more different layouts in the same controller)
我不喜欢使用
render :layout => 'name'
render :layout => 'name'
我喜欢做
布局 'name', :only => [:new]
layout 'name', :only => [:new]
但我不能用它来指定 2 个或更多不同的布局.
But I can't use that to specify 2 or more different layouts.
例如:
当我在同一个控制器中使用不同的布局名称和不同的唯一选项调用布局 2 次时,第一个被忽略 - 这些操作不会显示在我指定的布局中.
When I call layout 2 times in the same controller, with different layout names and different only options, the first one gets ignored - those actions don't display in the layout I specified.
注意:我使用的是 Rails 2.
Note: I'm using Rails 2.
推荐答案
您可以使用一个方法来设置布局.
You can use a method to set the layout.
class MyController < ApplicationController
layout :resolve_layout
# ...
private
def resolve_layout
case action_name
when "new", "create"
"some_layout"
when "index"
"other_layout"
else
"application"
end
end
end
这篇关于每个动作的 Rails 布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!