Rails默认布局文件被忽略

vbsql7

我的印象是,如果您在布局目录中有一个名为application.html.erb的文件,则该布局将自动应用于所有视图,而无需显式引用它。事实并非如此。

我有一个“家庭”控制器,它有一个“索引”方法:

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>

直到我在home控制器中添加对布局的明确引用,上述文件才会被忽略,如下所示:

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

是什么赋予了?我不需要命名每个控制器中使用的布局。那就是在应用程序级别上拥有它的意义。

列宁·拉杰·拉贾塞卡兰(Lenin Raj Rajasekaran)

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

class HomeController < ApplicationController
   def index
   end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章