昨天刚开始使用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 - &gt; codeigniter背景,我假设渲染类似于$ this-&gt; load-&gt; view('');在codeigniter 虽然这很好用,但我希望有多个应用程序布局文件,例如
在codeigniter中你只需要声明你想要使用哪些模板/布局文件,但是作为ruby on rails有点神奇(它为你做了很多事情),我假设它默认调用应用程序布局。我想知道是否有办法选择我想要的布局文件?
答案 0 :(得分:14)
@Deefour提供了正确的资源,这是一个很好的快速示例,说明如何在Rails 4中实现这一点。
在控制器中,您可以指定要为特定操作获取布局的位置,并对使用哪种布局进行非常精细的控制。
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标准布局文件。我认为它存在,它是默认的后备!
答案 1 :(得分:8)
您正在寻找layout
方法。
This Rails Guide会帮助您,特别是Finding Layouts。我在这里提供了更多细节,但前面提到的文档和指南提供了足够的示例和使用说明。