Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:48:44+00:00 2026-06-02T00:48:44+00:00

In my gem I only want to change the layout on a certain condition.

  • 0

In my gem I only want to change the layout on a certain condition.

I know I can have a method for specifying the layout, but how can I point to the current layout in that method? I’ve learned that _layout points to the layout name, but it causes a stack overflow if it’s called in the method that specifies the layout.

Here’s my code for clarification (In my engine’s application controller):

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: The application with this gem specifies a layout called ‘qday’, now the gem needs to change the layout if the path includes ‘baco’, but if it doesn’t, it should render ‘qday’.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:48:46+00:00Added an answer on June 2, 2026 at 12:48 am

    What you seem to be trying to do is always setting the layout, and if you don’t want to change it, setting it to what it already was. Instead, because of the recursion, I think you should only set the layout if your condition holds.

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

    It would be a cleaner design, however, if your engine controllers each called layout.

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

    That might not seem DRY, but it is a step cleaner.

    To fix the DRY thing, I’d used inheritance. Have a base controller class which sets the layout and inherit your other controllers from your base.

    class Baco::BaseController < ApplicaitonController
      layout "baco"
    end
    
    class Baco::BatsController < Baco::BaseContoller
      ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know it's a silly one but i want to know how can we
I would like to install a gem (JSON) on the client side, but only
I want to use Ruby/Mikel Mail gem to access pop3, but am stuck finding
rack-timeout is included in the Gemfile, but we only want it as middleware on
I want to know what version of a specific gem is working with the
I've searched the web, but I can only find information on sharing code between
I want to create a ruby gem that calls a c function I have
I want to use this gem in my api application https://github.com/seangeo/auth-hmac/ I have a
I want to install a gem on all my application servers, but gem install
This question will probably only make sense if you know about the whenever gem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.