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 505273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:34:54+00:00 2026-05-13T06:34:54+00:00

I am trying to create a bunch of dynamic helper methods like these: show_admin_sidebar

  • 0

I am trying to create a bunch of dynamic helper methods like these:

show_admin_sidebar
show_posts_sidebar
show_users_sidebar

So far I have this in my helper.rb file:

#spits out a partial 
def show_sidebar(name, show_sidebar = true)
  @content_for_sidebar = render :partial => "partials/#{name}"                       
  @show_sidebar = show_sidebar
end

def show_sidebar?
  @show_sidebar
end

In my application layout file I have this: (NB – I’m using HAML):

- if show_sidebar?
  = yield(:sidebar)

This allows me to say the following in my views:

- show_sidebar(:foo)
- show_sidebar(:bar)

And this renders the desired partial.

The problem with this is that I can only add one sidebar per page. So, I figure I need to have dynamic methods like: show_admin_sidebar, show_foo_sidebar.

So I have tried to do this:

def show_#{name}_sidebar(show_sidebar = true)
@name = name  
@content_for_#{@name}_sidebar = render :partial => "partials/#{@name}"                       
  @show_sidebar = show_sidebar
end

and then in my layout:

- if show_sidebar?
  = yield("{@name}_sidebar")

But rails does not like this at all.

I have tried almost everything I can think of in my helper file and nothing works.

The reason I am using helper methods for this is because I want my content div to be 100% page width unless there is a sidebar present in which case the main content goes into a smaller div and the sidebar content goes into it’s own..

If I can’t get this working, then I can easily fix the problem by just adding the partials manually but I’d like to get my head round this….

Anyone got any experience with this kind of thing?

  • 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-05-13T06:34:55+00:00Added an answer on May 13, 2026 at 6:34 am

    Here is a solution for you, however I wouldn’t suggest too much metaprogramming:

    #Add the following snippet to the proper helper module:
    ['admin','user','whatever'].each do |name|
      class_eval{
        "def show_#{name}_sidebar(show_sidebar = true)
           @name = #{name}  
           @content_for_#{@name}_sidebar = render :partial => 'partials/#{@name}'                       
           @show_sidebar = show_sidebar
         end"
      }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically create a bunch of class properties, but each dynamic fget
I'm trying to create a master index file for a bunch of HTML files
I have a WSDL file and I am trying to create a web service
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Trying to create a background-image slideshow and am getting this error... This is the
I am trying to create a bunch of directories/subdirectories that I can copy files
I am trying to use the output from mdfind to create a bunch of
I'm trying to create an application icon. I took a bunch of created *.ico
I'm trying to create a go board using opengl. To do this, I'm trying
Im trying to create a WCF-service to hold generic data for a bunch of

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.