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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:54:56+00:00 2026-05-21T03:54:56+00:00

My template looks like: <h2>Oracle</h2> <% @q_oracle.each do |q| %> <%= link_to(q.title + ‘

  • 0

My template looks like:

  <h2>Oracle</h2>

  <% @q_oracle.each do |q| %>
    <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>

  <% end %>


  <h2>Ruby and Rails</h2>

  <% @q_ruby.each do |q| %>
    <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>

  <% end %>

So the temlate consists the static titles (h2) and loops through the array. I am searching the way to avoid copy-paste code in the my template. Something like:

@hash = { 'Oracle' => @q_oracle, 'Ruby and Rails' => @q_ruby }

@hash.each { |@t, @a|

  <h2>@t</h2>

  <% @a.each do |q| %>
    <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>

  <% end %>
}

Is it possible?

  • 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-21T03:54:57+00:00Added an answer on May 21, 2026 at 3:54 am

    Yes you can do it.

    Ruby 1.9 solution

    In Ruby 1.8 this solution can be used when titles order doesn’t matter. In Ruby 1.9 titles would appear in order they were inserted in hash.

    Just place this variable to your controller action:

    @hash = { 'Oracle' => @q_oracle, 'Ruby and Rails' => @q_ruby }
    

    And access this variable from your view:

    <% @hash.each do |t, a| %>
      <h2><%= t %></h2>
      <% a.each do |q| %>
        <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>
      <% end %>
    <% end %>
    

    Ruby 1.8 with sorted keys

    This method sorts keys so they appear in alphabetical order.

    <% @hash.keys.sort.each do |t| %>
      <h2><%= t %></h2>
      <% @hash[t].each do |q| %>
        <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>
      <% end %>
    <% end %>
    

    Ruby 1.8 with arrays

    This method would behave like Ruby 1.9 in any ruby version – titles would appear in order they were added.

    Variable @hash must be initialized as:

    @hash = [ ['Oracle', @q_oracle], ['Ruby and Rails',@q_ruby] ]
    

    View must be updated to:

    <% @hash.each do |title_with_questions| %>
      <h2><%= title_with_questions[0] %></h2>
      <% title_with_questions[1].each do |q| %>
        <%= link_to(q.title + ' (' + q.answer_count.to_s + ') ' + q.question_id.to_s, 'http://stackoverflow.com/' + q.question_answers_url) %>  </br>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It looks like this template tag works like a charm for most people: http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/
My object looks like: public class Template { public string Title {get;set;} public string
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
In a template function that looks like this: template<typename T> constexpr T foo(T a,
I have code that looks like this: template<class T> class list { public: class
Suppose I have a function which looks like this: template <class In, class In2>
I want to have a template class that looks something like what I have
The spring JDBC template returns me a list which looks like below: [{CODE_C=CSC, DESC_C=CSC},
Absolute beginner question: I have a template file index.html that looks like this: ...

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.