Ruby on rails 为什么这会导致:show根本没有布局:布局';管理员';,:除外=>;[:show]

Ruby on rails 为什么这会导致:show根本没有布局:布局';管理员';,:除外=>;[:show],ruby-on-rails,layout,render,Ruby On Rails,Layout,Render,为什么这会导致:show根本没有布局:布局“admin”,:except=>[:show] 这是故意的行为吗?我被迫将render:layout=>“application”放在一个空的def show end中 show action不应该默认为基本布局吗?这让我有点吃惊,我不得不去源代码处查看。答案是否定的,它不应该默认为应用程序布局。相反,传递的条件用于确定该操作是否具有布局,因此称为mixed-in方法(Rails 3) 我有一半希望它的行为像respond\u to,您可以多次调用它来

为什么这会导致:show根本没有布局:布局“admin”,:except=>[:show]

这是故意的行为吗?我被迫将render:layout=>“application”放在一个空的def show end中


show action不应该默认为基本布局吗?

这让我有点吃惊,我不得不去源代码处查看。答案是否定的,它不应该默认为应用程序布局。相反,传递的条件用于确定该操作是否具有布局,因此称为mixed-in方法(Rails 3)

我有一半希望它的行为像
respond\u to
,您可以多次调用它来为不同的操作建立条件

在任何情况下,您都可以通过向包含逻辑的
layout
发送一个方法(通过proc或符号引用方法)来轻松处理此问题,而不是简单地定义一个空操作来呈现不同的布局

例如:

layout :determine_layout

...

def determine_layout
  # show gets application, the rest get admin
  params[:action] == 'show' ? 'application' : 'admin'

  # or, returning true would probably do it (and be more flexible in case 
  # the rest of your app swapped layouts to something other than application)
  params[:action] == 'show' || 'admin'
end

我得出了同样的结论。我觉得这种行为有点违反直觉。谢谢你的时间和帮助。