Reputation: 5178
I have the following helper method for the Contact
Model:
def list_offices_if_present(contact)
if contact.locations.any?
content_tag :span, "Works out of: #{contact.offices_list}" + tag(:br)
end
end
Here is that method definition called within that content_tag
:
#models/contact.rb
class Contact < ActiveRecord::Base
...
def offices_list
offices_names = []
locations.each{|location| office_names << location.office_name}
return office_names.join(", ")
end
end
I call this helper like this:
<p>
<%= list_offices_if_present(@contact) %>
<%= list_phone_if_present(@contact) %>
<p>
The issue is that the <br>
tag is rendering out as text, and not an actual line break, like this:
Works out of: Some Location <br /> Phone: 402-555-1234
How do I add a line break to the end of a content_tag within a helper method?
Upvotes: 2
Views: 2360
Reputation: 5178
content_tag(:span, "Works out of: #{contact.offices_list}" + raw("<br>"))
Upvotes: 1
Reputation: 1077
I think your problem is the following line of code
content_tag :span, "Works out of: #{contact.offices_list}" + tag(:br)
is executed as
content_tag(:span, "Works out of: #{contact.offices_list}" + tag(:br))
Notice tag(:br)
is concatenated with "Works out of: #{contact.offices_list}"
as second argument.
To fix it, add an explicit pair of parenthesis like this:
content_tag(:span, "Works out of: #{contact.offices_list}") + tag(:br)
Upvotes: 0
Reputation: 44581
Rails automatically escapes html entities, you can use:
content_tag :span, "Works out of: #{contact.offices_list}".html_safe + tag(:br)
Upvotes: 1