gpt4 book ai didi

css - 忽略 Rails 默认布局文件

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:19 25 4
gpt4 key购买 nike

我的印象是,如果您的 layouts 目录中有名为 application.html.erb 的文件,则此布局将自动应用于所有 View ,而无需明确引用它。情况似乎并非如此。

我有一个“家庭” Controller ,它有一个“索引”方法:

class HomeController < ActionController::Base
def index
end
end

和相关的home.html.erb查看页面:

<h2>Welcome!</h2>
<div>Stay tuned for basic functions to start arriving on this site.</div>
<div>The site will not look very stylish until one of the bounties gets done about writing the Style guide.</div>

最后是位于布局中的 application.html.erb 文件:

<!DOCTYPE html>
<html>
<head>
<title>App Title</title>
<%= stylesheet_link_tag "application", media: "all" %>
</head>
<body>
<div class="navbar">
<div class="navbar-inner">
<a href="/categories/index">Categories</a>
</div>
</div>

<%= yield %>

<div class="footer">Michael</div>
</body>
</html>

上面的文件被忽略,直到我像这样在我的家庭 Controller 中添加对布局的显式引用:

class HomeController < ActionController::Base
layout 'application'
def index
end
end

什么给了?我不想为我在每个 Controller 中使用的布局命名。这就是在应用程序级别拥有它的意义所在。

最佳答案

问题是您继承自 ActionController::Base。您需要从 ApplicationController 继承子类以要求 Rails 使用“应用程序”布局作为默认布局。

class HomeController < ApplicationController
def index
end
end

关于css - 忽略 Rails 默认布局文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27640587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com