2014-12-24 104 views
0

我的印象是,如果您的佈局目錄中的文件名爲application.html.erb,則此佈局會自動應用於所有視圖而無需明確引用它。 這似乎並非如此。忽略Rails默認佈局文件

我有一個 '家' 控制器,它有一個 '指數' 的方法:

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> 

上述文件被忽略了,直到我在我的家鄉控制器這樣增加了一個明確的參考佈局:

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

是怎麼回事?我不想在每個控制器中命名我使用的佈局。這就是在應用程序級別擁有它的關鍵。

+0

rails version? – emaillenin

回答

4

問題是你從ActionController :: Base繼承。您需要從ApplicationController子類來請求Rails使用「應用程序」佈局作爲默認值。

class HomeController < ApplicationController 
    def index 
    end 
end 
+0

可愛的。謝謝。 – vbsql7