2015-04-01 65 views

回答

4

創建新的佈局如app/views/layouts/dunno.html.erb。使用在控制器

class DashboardController < ApplicationController 
    layout 'dunno' 
end 

或每行動

class DashboardController < ApplicationController 
    def index 
    render layout: 'dunno' 
    end 
end 

看到docs的細節

0

可以在應用控制器做到這一點,添加以下代碼,我假設你正在使用devise

layout :layout_by_resource 

def layout_by_resource 
    user_signed_in? ? "my_custom_layout" : "application" 
end 
0

約佈局細節可以發現here

使用在動作不同的佈局渲染調用

如果你的大多數動作都使用相同的佈局,它非常有意義如上所述 定義一個控制器範圍的佈局。有時候,你會在 有一些例外,其中一個動作想要使用與控制器的其餘部分不同的佈局。您可以通過向渲染調用傳遞:layout 選項來完成此操作。例如:

class WeblogController < ActionController::Base 
    layout "weblog_standard" 

    def help 
    render action: "help", layout: "help" 
    end 
end 

這將覆蓋控制範圍的「weblog_standard」佈局,將呈現與「幫助」佈局的幫助 動作來代替。

1

在你的application_controller.rb文件中做這個,希望它有幫助。

layout :set_layout 
def set_layout 
    if current_user 
     'dashboard_layout' 
    else 
     'default_layout' 
    end 
end 
0

如果您正在使用色器件寶石,而你的目標是使用內其他佈局設計控制器,看看他們的docs

0

我登入前,登入後需要2個不同的佈局,所以我已經實現了使用下面的代碼,其中application_controller更改佈局,如果用戶signed_in else使用不同的佈局.... 如果您正在使用設計,不要忘記添加布局在視圖/佈局

在application_controller.rb

layout :layout_by_resource 
    def layout_by_resource 
    unless user_signed_in? 
     Rails.logger.info "===========Setting layout as views/layouts/auth.html.erb" 
     'auth' 
    else 
     Rails.logger.info "===========Setting layout as views/layouts/blue.html.erb" 
     'basic' 
    end 
    end 
相關問題