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

  • Home
  • SEARCH
  • 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 an object file which can be useable in any computer.
I'm trying to create a bunch of entries in a database with a single
I have a python script that is trying to create a directory tree dynamically
So I am trying to create a search that returns a bunch of results
I am trying to create a JSON object from a bunch of grouped HTML
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference
Trying to create my first iPhone app that would play back audio. When I

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.