2013-03-05 36 views
7

昨天剛剛開始Ruby on Rails。在我的佈局/ application.html.erb我有:ror中的多個佈局

<!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編輯 - >笨的背景下,我假設呈現類似於被$ this->負載>視圖( '');在codeigniter中。
雖然這工作得很好,我想有多個應用程序佈局文件,例如

  1. 佈局/應用程序的默認
  2. 佈局/應用全角(全角爲頁)
  3. 等..

在codeigniter中,你只需聲明你希望使用哪個模板/佈局文件,但在rails上的ruby有點神奇(它爲你做了很多事情),我假設它調用應用程序佈局德故障。我想知道是否有辦法選擇我想要的佈局文件?

回答

8

您正在尋找layout方法。

This Rails Guide將幫助你,具體Finding Layouts。我會在這裏提供更多的細節,但前面提到的文檔和指南提供了足夠多的示例和使用說明。

+0

哦謝謝,我還以爲我讀完了那頁。 – Skyalchemist 2013-03-05 16:14:18

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是鐵軌標準配置文件。我認爲它是存在的,它是默認的後備!

+0

所有未定義的將回退到application.html.erb? – 2015-06-20 14:59:20

+0

'''application.html.erb'''是rails標準佈局文件。我假設它存在,並且它是默認的回退,是的。 – mahatmanich 2015-06-21 19:54:33