昨天刚开始 Ruby on Rails。在我的 layouts/application.html.erb 我有:
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
来自 php ->codeigniter 背景,我假设 render 类似于 $this->load->view('');在代码点火器中。
虽然这很好用,但我想要多个应用程序布局文件,例如
在 codeigniter 中,您只需声明您希望使用哪个模板/布局文件,但由于 ruby on rails 有点神奇(它为您做了很多事情),我假设它默认调用应用程序布局。我想知道是否有办法选择我想要的布局文件?
最佳答案
@Deefour 提供了正确的资源,这是一个很好的快速示例,说明如何在 Rails 4 中实现这一点。
在 Controller 中,您可以指定要在何处获取特定操作的布局,并对使用的布局进行非常精细的控制。
class PagesController < ApplicationController
layout "fullwidth", except: [:index, :faqs]
def popout
# Render this action with specific layout
render layout: "popout"
#renders with views/layouts/popout.html.erb
end
def index
#renders with views/layouts/application.html.erb
end
def about_us
#renders with views/layouts/fullwidth.html.erb
end
def team
#renders with views/layouts/fullwidth.html.erb
end
def logo
#renders with views/layouts/fullwidth.html.erb
end
def faqs
#renders with views/layouts/application.html.erb
end
end
application.html.erb 是 rails 标准布局文件。我假设它存在,并且它是默认的后备!
关于ruby-on-rails - rr 中的多个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228228/