Ruby-on-rails 每个动作的 Rails 布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3025784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails layouts per action?
提问by mrbrdo
I use a different layout for some actions (mostly for the new action in most of the controllers).
我对某些操作使用了不同的布局(主要用于大多数控制器中的新操作)。
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)
我想知道指定布局的最佳方法是什么?(我在同一个控制器中使用了 3 个或更多不同的布局)
I don't like using
我不喜欢使用
render :layout => 'name'
渲染:布局 => '名称'
I liked doing
我喜欢做
layout 'name', :only => [:new]
布局 'name', :only => [:new]
But I can't use that to specify 2 or more different layouts.
但我不能用它来指定 2 个或更多不同的布局。
For example:
例如:
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.
当我在同一个控制器中使用不同的布局名称和不同的唯一选项调用布局 2 次时,第一个被忽略 - 这些操作不会显示在我指定的布局中。
Note: I'm using Rails 2.
注意:我使用的是 Rails 2。
回答by August Lilleaas
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
回答by axeltaglia
回答by Gavin Terrill
You can specify the layout for an individual action using respond_to:
您可以使用respond_to为单个操作指定布局:
def foo
@model = Bar.first
respond_to do |format|
format.html {render :layout => 'application'}
end
end
回答by yottanami
You can also specify the layout for action using render:
您还可以使用渲染指定操作的布局:
def foo
render layout: "application"
end
回答by barelyknown
There's a gem (layout_by_action) for that :)
有一个 gem (layout_by_action) :)
layout_by_action [:new, :create] => "some_layout", :index => "other_layout"
回答by puneet18
Various ways to specify layout under controller:
在控制器下指定布局的各种方法:
In following code, application_1 layout is called under index and show action of Users controller and application layout(default layout) is called for other actions.
class UsersController < ApplicationController layout "application_1", only: [:index, :show] endIn following code, application_1 layout is called for all action of Users controller.
class UsersController < ApplicationController layout "application_1" endIn following code, application_1 layout is called for test action of Users controllers only and for all other action application layout(default) is called.
class UsersController < ApplicationController def test render layout: "application_1" end end
在下面的代码中,application_1 布局在索引下被调用,用户控制器的显示动作和应用程序布局(默认布局)被调用以用于其他动作。
class UsersController < ApplicationController layout "application_1", only: [:index, :show] end在以下代码中,为用户控制器的所有操作调用 application_1 布局。
class UsersController < ApplicationController layout "application_1" end在下面的代码中,application_1 布局仅被调用用于用户控制器的测试操作,而对于所有其他操作应用程序布局(默认)被调用。
class UsersController < ApplicationController def test render layout: "application_1" end end
回答by Gregdebrick
Precision :
精确 :
A not really but working DRY way is what you see above, but with a precision : the layout need to be after your variables for working("@some"). As :
您在上面看到的不是真正但有效的 DRY 方式,但具有精确性:布局需要在您的工作变量之后(“@some”)。作为 :
def your_action
@some = foo
render layout: "your_layout"
end
And not :
并不是 :
def your_action
render layout: "your_layout"
@some = foo
@foo = some
end
If you do a before_action... it's won't work also.
如果您执行 before_action ... 它也不会起作用。
Hope it helps.
希望能帮助到你。

