Rails- nested content_tag

Ruby on-RailsRuby on-Rails-3Helpers

Ruby on-Rails Problem Overview


I'm trying to nest content tags into a custom helper, to create something like this:

<div class="field">
   <label>A Label</label>
   <input class="medium new_value" size="20" type="text" name="value_name" />
</div>

Note that the input is not associated with a form, it will be saved via javascript.

Here is the helper (it will do more then just display the html):

module InputHelper
    def editable_input(label,name)
	     content_tag :div, :class => "field" do
	   	  content_tag :label,label
		  text_field_tag name,'', :class => 'medium new_value'
	     end
    end
end

<%= editable_input 'Year Founded', 'companyStartDate' %>

However, the label is not displayed when I call the helper, only the input is displayed. If it comment out the text_field_tag, then the label is displayed.

Thanks!

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You need a + to quick fix :D

module InputHelper
  def editable_input(label,name)
    content_tag :div, :class => "field" do
      content_tag(:label,label) + # Note the + in this line
      text_field_tag(name,'', :class => 'medium new_value')
    end
  end
end

<%= editable_input 'Year Founded', 'companyStartDate' %>

Inside the block of content_tag :div, only the last returned string would be displayed.

Solution 2 - Ruby on-Rails

You can also use the concat method:

module InputHelper
  def editable_input(label,name)
    content_tag :div, :class => "field" do
      concat(content_tag(:label,label))
      concat(text_field_tag(name,'', :class => 'medium new_value'))
    end
  end
end

Source: Nesting content_tag in Rails 3

Solution 3 - Ruby on-Rails

I use a variable and concat to help with deeper nesting.

def billing_address customer
  state_line = content_tag :div do
    concat(
      content_tag(:span, customer.BillAddress_City) + ' ' +
      content_tag(:span, customer.BillAddress_State) + ' ' +
      content_tag(:span, customer.BillAddress_PostalCode)
    )
  end
  content_tag :div do
    concat(
      content_tag(:div, customer.BillAddress_Addr1) +
      content_tag(:div, customer.BillAddress_Addr2) +
      content_tag(:div, customer.BillAddress_Addr3) +
      content_tag(:div, customer.BillAddress_Addr4) +
      content_tag(:div, state_line) +
      content_tag(:div, customer.BillAddress_Country) +
      content_tag(:div, customer.BillAddress_Note)
    )
  end
end

Solution 4 - Ruby on-Rails

building nested content tags with iteration is a little different and gets me every time... here is one method:

      content_tag :div do
        friends.pluck(:firstname).map do |first| 
          concat( content_tag(:div, first, class: 'first') )
        end
      end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionchristo16View Question on Stackoverflow
Solution 1 - Ruby on-RailsPeterWongView Answer on Stackoverflow
Solution 2 - Ruby on-RailslmikaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsSean MView Answer on Stackoverflow
Solution 4 - Ruby on-RailsSean MView Answer on Stackoverflow