Create links with gsub

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.

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. [email protected] wrote:

product.description.gsub(/<a=‘’'>/, “”)

To unsubscribe from this group, send email to
[email protected]
[email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Sandip


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/

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.