# Create links with gsub

**URL:** <https://www.ruby-forum.com/t/create-links-with-gsub/185062>\
**Category:** Rails\
**Created:** [March 12, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/create-links-with-gsub/185062 "2010-03-12T20:27:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [March 12, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/create-links-with-gsub/185062/1 "2010-03-12T20:27:57Z")

</div>

Hello,

So I have a text area that displays a products description, in the  
products description I would like to be able to create links to other  
products through the product’s id. Mainly because the url’s will  
change from time to time and I would hate to have random broken links  
on my site. Anyways so far I have this in my helper.

module ProductsHelper  
def product\_description(product)  
product.description.gsub(/\<a=’’’\>/, “”)  
end

and then in my view I have this;

\<%= product\_description(@product) %\>

Now when editing the text area for the products description, I would  
like to do something like this;

This is a test \<a=12345\>Product x

So I would like gsub to be able to get the product id which would be  
12345 and then replace that product id with the product.permalink  
field that is associated with that product.id. as you can tell I am  
totally lost on where to even go from here. any help would be greatly  
appreciated. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [March 12, 2010, 10:40pm UTC](https://www.ruby-forum.com/t/create-links-with-gsub/185062/2 "2010-03-12T22:40:13Z")

</div>

Hi  
Sorry but It would be good for future if you look at  
ruby regular expressions instead of asking for any solutions.

Here is regular expression for gsub

product.description =~ /href="(\d+)/  
product.description.gsub($1, product.url)

My suggestion is that use _$PRODUCT\_URL\__ instead of link in  
description  
of product  
and then use gsub to render link in view.

_product.description.gsub(/$PRODUCT\_URL\_\<\d+\>/, product.url )_

On Sat, Mar 13, 2010 at 12:56 AM, Dan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> product.description.gsub(/\<a=‘’'\>/, “”)
> 
>  
> 
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Sandip

* * *

> **[Fun On Rails](http://funonrails.com/)**
>
> Journal of a Web Developer #ruby #rails #JS

---

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [March 12, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/create-links-with-gsub/185062/3 "2010-03-12T23:17:41Z")

</div>

Thanks for the reply, just in case anyone comes across this and needs  
a solution I found this little write up by Arik;

[http://blog.atinypixel.com/2009/05/designer-on-rails-the-gsub-method/](http://blog.atinypixel.com/2009/05/designer-on-rails-the-gsub-method/)

still working through the details so I don’t have the exact solution  
yet, but fairly close. Hope this helps anyone else that is looking to  
use gsub in the text to replace text with a link.
