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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:26:57+00:00 2026-05-14T05:26:57+00:00

I want to print some objects in a table having 2 rows per object,

  • 0

I want to print some objects in a table having 2 rows per object, like this:

<tr class="title">
    <td>Name</td><td>Price</td>
</tr>
<tr class="content">
    <td>Content</td><td>123</td>
</tr>

I wrote a helper method in products_helper.rb, based on the answer of this question.

def write_products(products)
  products.map {
    |product|
    content_tag :tr, :class => "title" do
      content_tag :td do
        link_to h(product.name), product, :title=>product.name
      end
      content_tag :td do
        product.price
      end
    end
    content_tag :tr, :class => "content" do
      content_tag :td, h(product.content)
      content_tag :td, product.count
    end
  }.join
end

But this does not work as expected. It only returns the last node – the last <td>123</td>

What should I do to make it work?

  • 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-14T05:26:57+00:00Added an answer on May 14, 2026 at 5:26 am

    Remember the function content_tag returns a string. It doesn’t write directly to the page. So what you are doing with the TDs is this:

    content_tag :tr do
      content_tag :td do
        link_to h(product.name), product, :title=>product.name
      end
      content_tag :td do
        product.price
      end
    end
    

    Which if we evaluate this partially would be

    content_tag :tr do
      "<td title='Ducks'> <a ...>Ducks</a></td>"
      "<td>19</td>"
    end
    

    And in a block, the last value is the one that is returned. There are two strings present, but the first one just gets lost in the ether. The second string is the last value in the block and gets returned.

    What you need to do is place a + between them to add the strings together :

    content_tag :tr do
      (content_tag(:td) do
        link_to h(product.name), product, :title=>product.name
      end) + #SEE THE PLUS IS ADDED HERE
      (content_tag(:td) do
        product.price
      end)
    end
    

    You must do the same at the TR level, just put a plus in there after the end of the first content_tag.

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

Sidebar

Related Questions

I want to print out some debug messages in my erb template when it
I Want Print DataBase Schema in Microsoft Sql Server Compact Edition. Maybe Exist Some
If I have some text that I want to print out on a page,
I want to print all the prime numbers between two numbers. This is my
What I have is this object.innerHTML which is this: <TABLE > <TBODY> <TR> <TD
My question is similar to this one but I'm having some problems with the
I want to visualize some data about cars in a table via Prefuse Java.
I want to print the first 10000 prime numbers. Can anyone give me the
I want to print the full length of a C-string in GDB. By default
I want to print styled html pages with their images from a script. Can

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.