控制器中的不同渲染布局未在轨道中正确加载

时间:2014-01-11 14:20:39

标签: ruby-on-rails layout controller

在我的控制器中我有2个布局,但是在重定向后布局没有改变?我不仅仅是行动:new_line_items使用布局“应用程序”而另一个使用布局“front_layout”

我的控制员:

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 !@store.blank?
    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

1 个答案:

答案 0 :(得分:0)

摆脱你的compute_layout方法并更改layout :compute_layout' to布局'front_layout`。这将为所有操作设置默认布局。

要更改单个操作的布局,请执行以下操作:

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