# Simple\_format, (long) urls, and wrapping

**URL:** <https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168>\
**Category:** Rails\
**Created:** [July 26, 2008, 2:49am UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168 "2008-07-26T02:49:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 26, 2008, 2:49am UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168/1 "2008-07-26T02:49:22Z")

</div>

I’m using simple\_format to format and display posted content from  
users. This content sometimes has urls in it.

The problem I’m having is that if the urls are long enough to wrap,  
they do, but the url gets split with a   
 tag. Then, if the user  
clicks on the link, only the first, non-wrapped part of the link gets  
posted to the browser… and that’s an invalid link.

Is there a better way to handle this so that the link wraps, but keeps  
its integrity?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 26, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168/2 "2008-07-26T18:17:18Z")

</div>

Have you tried to use auto\_link with simple\_format? This, I would  
think, would keep your link integrity, even if there was a line break.  
Let me know if that works.

~Dustin T.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [July 27, 2008, 2:25am UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168/3 "2008-07-27T02:25:26Z")

</div>

Yeah, that’s exactly what I do, as follows:

simple\_format(sanitize(auto\_link(content, :all, :target =\> ‘\_blank’)))

And that’s what ends up with the break.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [September 8, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168/4 "2008-09-08T19:08:41Z")

</div>

touching this again 'cause I never found a solution and it just came  
back and bit me

this must be a common problem and perhaps I’ve missed something  
obvious…?

any more thoughts/help out there?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [September 8, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/simple-format-long-urls-and-wrapping/144168/5 "2008-09-08T23:03:43Z")

</div>

OK… sorry for the thrash, but I’ve found a solution of sorts and  
thought I’d share…

* * *

Turns out my problem was not with simple\_format. Instead it was with  
the TextHelper word\_wrap call I’d been using. When it wraps long bits  
of unbroken text (like a long url), it inserts   
 tags. That was  
screwing things up.

So, I went in search of a solution that might wrap text using CSS.  
Problem is that the solutions dont work cross-browser. But I found  
something that works that doesnt use CSS (or Ruby code), it’s JS.

Check it out: [http://www.hedgerwow.com/360/dhtml/css-word-break.html](http://www.hedgerwow.com/360/dhtml/css-word-break.html)

I put the breakWord function in my application.js file, added another  
function that looks for my DOM elements by class name (I name the  
relevant ones ‘content’), and then call that function from the onLoad  
event in my application layout. Works well, though others may have a  
more elegant way to do this.

Hope that helps.
