Jake
Jake

Reputation: 1370

In Rails how to create a new line on text being displayed?

I have a client that it putting in data into a text field that needs to be separated into a new line after a certain character. Basically they are giving me this:

7:30 AM - 5:00 PM (M-F) 7:30 AM - 12 PM (Sat)

I'm displaying it in the view as:

<% @region.locations.each do |location| %>
  <%= location.hours_operation %>
<% end %>

What I really need is after to a line break after the ")" or before the number. How do I create the line break?

Upvotes: 1

Views: 45

Answers (1)

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33420

You could split your string with a regular expression that catches the space between your first parenthesis and the AM hour right after it and then join it with "\". Or also use gsub for that:

str = '7:30 AM - 5:00 PM (M-F) 7:30 AM - 12 PM (Sat)'

puts str.split(/(?<=\) )/).join("\n")
# 7:30 AM - 5:00 PM (M-F) 
# 7:30 AM - 12 PM (Sat)

puts str.gsub(/(?<=\) )/, "\n")
# 7:30 AM - 5:00 PM (M-F) 
# 7:30 AM - 12 PM (Sat)

Upvotes: 2

Related Questions