content_tag, oddly nested, rails 4

241 Views Asked by At

Trying to create the below using content_tag:

<p><span class="field-label">Address: </span><%= @patient.address1 %></p>

<%= content_tag :p, @patient.address1 do %>
  <%= content_tag :span, "Address: ", :class => 'field-label'%>
<%end%>

This returns:

<p><span class='field-label>Address: </span></p>
1

There are 1 best solutions below

0
On

As mentioned in the docs, if you use the block format then the second argument becomes the options. Ie. you can't mix the content as a parameter and the content as a block. So to solve this you need to put your @patient.address1 within the block:

<%= content_tag :p do %>
  <%= content_tag :span, "Address: ", :class => 'field-label' %>
  <%= @patient.address1 %>
<% end %>