2014-01-11 44 views
0

在我的控制器我有2佈局,但是當重定向後佈局不改變?我不會只是動作:不同的渲染布局在控制器中不加載porperly在軌道上

class HomeController < ApplicationController  
    layout :compute_layout 

    before_filter :collect_menu, :only => [:show_item, :location, :order_items, :new_line_items] 

    def index 
    @stores = Store.all 
    if !params[:store_id].blank? 
     @store = Store.find(params[:store_id]) 
     menu_left_all(params[:store_id]) if [email protected]? 
    end 
    end 

    def show_item 
    @store = Store.find(params[:store_id]) 
    item_childs(params[:id]) 
    end 

    def location 
    @store = Store.find(params[:store_id]) 
    end 

    def new_line_items 

    end 

    def create_line_items  
    @store = Store.find(params[:store_id]) 
    item = Item.find(params[:id])   
    line_items = item.line_items.build(quantity: params[:quantity]) 

    respond_to do |format| 
     if line_items.save 
     format.html { redirect_to stores_url, notice: 'Line Items was successfully created.' } 
     end 
    end 
    end 

    def compute_layout 
    if request.url.include?("new_line_items") 
     action_name = "application" 
    else 
     action_name = "front_layout" 
    end 
    end 
end 

回答

0

擺脫你compute_layout方法,改變layout :compute_layout' to佈局「front_layout`:使用佈局「應用程序」,另一個使用佈局「front_layout」

我的控制器new_line_items。這將爲所有操作設置默認佈局。

要爲單個動作改變佈局做:

def new_line_items 
    render layout: 'application' 
end 

另一種解決方案:

如果您想佈局可能會改變未來,你應該讓你的compute_layout的方法,但改變它:

def compute_layout 
    if action_name == "new_line_items" 
    "application" 
    else 
    "front_layout" 
    end 
end 
+0

謝謝解決我的問題 – tardjo