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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:23:56+00:00 2026-05-13T17:23:56+00:00

I have a pattern in a couple of templates that’s repeated. The code is

  • 0

I have a pattern in a couple of templates that’s repeated. The code is for rendering a set of tabs and content for each tab.

Contents of /app/views/general/_product_groupings.html.erb

<table cellpadding="1" cellspacing="0" class="sub_container clear">
  <tr>
    <% first_visible_tab = true %>
    <% @bundle.groupings.each do |group| %>
      <td id="tab_heading_for_group_<%= group.id %>" class="tab_heading <%= 'selected' if first_visible_tab %>" onclick="show_tab('product_type_<%= group.id %>')"><%= one_liner(group.name) %></td>
      <td></td>
      <% first_visible_tab = false %>
    <% end %>
    <td class="last"></td>
  </tr>
  <tr>
    <td colspan="99" class="tab_content">

      <% first_visible_tab = true %>
      <%# groupings is an array of products %>
      <% @bundle.groupings.each do |group| %>
        <div style="display: <%= (first_visible_tab) ? '' : 'none' %>" id="tab_body_for_group_<%= group.id %>" class="tab_body container_inner">
          <% first_visible_tab = false %>
          <% template = case group.grouping_type 
            when 'selection'
              :product_selection_group
            when 'group'
              :product_optional_group
            when 'product'
              :product_optional_group
            end %>
            <%= render :partial => template.to_s, :locals => {:group => group} %>


        </div>
      <% end %>
    </td>
  <tr>
</table>

The code consists of some parts. There is the general parameter passed in, @bundle. There is a header section (lines 1-10):

<table cellpadding="1" cellspacing="0" class="sub_container clear">
  <tr>
    <% first_visible_tab = true %>
    <% @bundle.groupings.each do |group| %>
      <td id="tab_heading_for_group_<%= group.id %>" class="tab_heading <%= 'selected' if first_visible_tab %>" onclick="show_tab('product_type_<%= group.id %>')"><%= one_liner(group.name) %></td>
      <td></td>
      <% first_visible_tab = false %>
    <% end %>
    <td class="last"></td>
  </tr>

In the header section, there are parts that differ each place code like this is used: The collection iterated upon, @bundle.groupings. The parameter for show_tab() onclick. The id of the tab id="tab_heading_for_group_<%= group.id %>".

Below the header, there is an area that i reckon could be yielded as a block, the real content of each tab content area (lines 19-27):

          <% template = case group.grouping_type 
            when 'selection'
              :product_selection_group
            when 'group'
              :product_optional_group
            when 'product'
              :product_optional_group
            end %>
            <%= render :partial => template.to_s, :locals => {:group => group} %>

Just above the content is the same collection @bundle.groupings.each do ... repeated from the head section.

I would really much like to DRY this up. By defining a block with the content that can be yielded inside a helper method.

I think this method should take the following params:

  • id_template
  • onclick_method #perhaps with a pattern for the parameter to the onclick method
  • collection
  • block to be yielded inside the content area of the tab

Question is, how do I make a helper method to take both block and params?

  • 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-13T17:23:56+00:00Added an answer on May 13, 2026 at 5:23 pm

    After reading Coda Hales examples on blocks, I found a solution that worked. The trick is to add your usual parameters, followed by the block like so:

    method_call(params) { |x| x.do_stuff_inside_the_methods_context }
    

    And x will be given the scope of the yield inside your method. Here is the rewrite of the code used in the example. (for now, leaving out any dynamic parameters to the onclick method)

    View:

    <% options = {
      :iterator => @bundle.groupings,
      :id_prefix => "group"
    } %>
    <%= render_tabs(options) { |product|  
      template = case product.grouping_type 
                when 'selection'
                  :product_selection_group
                when 'group'
                  :product_optional_group
                when 'product'
                  :product_optional_group
                end
      render :partial => template.to_s, :locals => {:group => product}
    
    } %>
    

    Helper:

      def render_tabs(opts, &block)
        collection = opts[:iterator]
        prefix     = opts[:id_prefix]
        buf = %(
        <table cellpadding="1" cellspacing="0" class="sub_container clear">
          <tr>
        )
        first_visible_tab = true 
        collection.each do |iter| 
          classnames = "tab_heading"
          classnames << " selected" if first_visible_tab
          td_id = "#{prefix}_#{iter.id}"
          buf << %(
            <td id="tab_heading_for_#{td_id}" class="#{classnames}" onclick="show_tab('#{td_id}')">#{one_liner(iter.name) }</td>
          )
    
          first_visible_tab = false 
        end 
        buf << %(<td class="last"></td>
          </tr>
          <tr>
            <td colspan="99" class="tab_content">)
    
        first_visible_tab = true 
        collection.each do |iter|
          td_id = "#{prefix}_#{iter.id}"
          display_attr = (first_visible_tab) ? '' : ' style="display:none"'
          buf << %(
            <div #{display_attr} id="tab_body_for_#{td_id}" class="tab_body container_inner">
            )
          first_visible_tab = false
    
          buf << yield(iter)
          buf << %(
            </div>
            )
        end
        buf << %(
            </td>
          <tr>
        </table>
        )
    
        buf
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses the shared gateway pattern to implement an inversion
I have a couple of classes that want to pass each other some information
I have the following piece of code pattern: void M1(string s, string v) {
I have looked over the Repository pattern and I recognized some ideas that I
I have the following code for factory design pattern implementation. class Pen{ public: virtual
Have you tried using MVC or any other UI pattern for GWT client code.
I have a model that already has a couple dozen of columns that will
I have a pattern to match with the string: string pattern = @asc I
No, this is not a question about generics. I have a Factory pattern with
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the

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.