Only change the layout under a certain condition

In my gem, I only want to change the layout in a certain state.

I know that I may have a method for specifying a layout, but how can I point to the current layout in this method? I found out that it _layoutpoints to the name of the layout, but causes a stack overflow if it calls a method that points to the layout.

Here is my code for clarification (in my application controller for engines):

layout :get_layout

def get_layout

  current = _layout # this is what I want, but causes a stack overflow
  request.path_info.include?( '/baco/' ) ? 'baco' : current

end

So, for example: An application with this gem indicates a layout called "qday", now the stone should change the layout if the path includes "baco", but if this does not happen, it should display "qday".

Thank!

+3
source share
2 answers

, , - , , , . , - , , , .

before_filter set_baco_layout

def set_baco_layout
  self.class.layout "baco" if request.path_info.include?( '/baco/' )
end

, , .

class Baco::BatsController < ApplicationController
  layout "baco"
  ...

, .

, . , .

class Baco::BaseController < ApplicaitonController
  layout "baco"
end

class Baco::BatsController < Baco::BaseContoller
  ...
+8

Try:

before_filter only: [:index, :show, :edit, :new] do
  render layout: 'baco' if request.path_info.include? '/baco/'
end

BTW, Rails . , controller (, users.html.haml) /views/layouts/. ...

-1

All Articles