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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:23:05+00:00 2026-05-26T21:23:05+00:00

I have a layout with common elements in the side bar per page (some

  • 0

I have a layout with common elements in the side bar per page (some of the elements are common to most pages, some are unique for some pages).

--------------------------------
|                   | side box |
|                   |          |
|                   |__________|
|                   | side box |
|                   |          |
|                   |__________|
|                   | side box |
|                   |          |
|                   |          |
--------------------------------

Right now the way I do it is include a shared/sidebar_type partial from the views. The sidebar partial further includes the individual box partials that type of page should display.

1) What’s the best way to segment and include these partials and customize which boxes of content are shown on the side page based on each page?

2) Best practice for setting objects and passing them onto those partials (the sidebar boxes) for output

For data used by the side boxes I’m referring to here. Should it go in a master controller as global variables which all other controllers inherit? etc.

I’m new to Rails from the PHP world and proper organization of these shared partials is slightly confusing. The way I’m currently passing objects to those box partials seems half-assed.

  • 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-26T21:23:05+00:00Added an answer on May 26, 2026 at 9:23 pm

    Well. Just an idea: You could have a _side_bar partial which, in turn, includes all the _side_box partials determined in an array initialized in the controller method. So you could have something like this:

    Controller:

    ...
    def some_method
        @side_boxes = ["side_box_1", "side_box_2", "side_box_3"]
        ...
    end
    ...
    

    some_method.html.erb (or in layouts/application.html.erb if you want it in all pages):

    ...
    <%= render :partial => 'shared/side_bar' %>
    ...
    

    _side_bar.html.erb:

    ...
    <% @side_boxes.each do |partial_name| %>
        <%= render :partial => "shared/#{parial_name}" %>
    <% end %>
    ...
    

    Then you’d have to have _side_box_1.html.erb, _side_box_2.html.erb, etc…

    To answer the second part of your question: if you want to pass arguments to a partial, you can give the hash parameter :locals to the render function. Like so:

    <%= render :partial => 'foo/bar', :locals => {:arg_1 => "argument1", :arg_2 => "argument2"} %>
    

    Then you can use those parameters in the _bar.html.erb partial like local variables:

    <%= arg_1 + " " + arg_2 %>
    #=> argument1 argument2
    

    Hope this helps!

    EDIT

    If the sidebars are going to be rendered throughout the site, you might want to have a method get_side_boxes in your ApplicationHelper (app/helpers/application_helper.rb). This way, you just put this in your layouts/application.html.erb:

    <% @side_boxes = get_side_boxes %>
    

    and it’ll be called every time a page loads. Then in your ApplicationHelper, you’d have to define the behavior of that function so it gets the side_boxes for the specific controller action you’re rendering. It would be something like this:

    ApplicationHelper

    def get_side_boxes
       action = params[:action]
       controller = params[:controller]
       model = controller.classify #This will return the class name corresponding to that controller
       model.constantize.get_side_boxes(action) #This will return whatever the CurrentModelName.get_side_boxes returns
    end
    

    Then in each model (or at least the ones that have controllers that render side_boxes), you’d have to implement a get_side_boxes method that returns the appropriate array of partial names depending on the action that is passed to it. Something like

    SomeModel

    def self.get_side_boxes(action)
        side_boxes = Array.new
        case action
            when "index"
                side_boxes = ["side_box_1", "side_box_2"]
            when "new"
                side_boxes = ["side_box_3"]
            when "edit"
                ...
            ...
        end
        side_boxes
    end
    

    Then you wouldn’t have to call anything in the controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Situation: I have some custom components in my layout. I have a common
I have a pretty common layout issue that I have traditionally used a table
I have a layout as follows: <div style=width: 300> <div style=width: 300>Some Content</div> </div
Looking for some implementation advice: I have a page that has a 3-tab ajaxToolkit:TabContainer.
I have two questions regarding to html table layout. First I will provide some
I have a layout.html.erb file w hich should act as a common file for
I'm using GWT 2.3 and have what I think is an extremely common layout
We have a multi module maven project with the following layout project - common
Is it possible to have a regular html page inherit its layout from another
I have some 20 sites with common codebase and database via Drupal Multisite installation.

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.