Topic: Split and make links

I'm having some troubles again...

I have a column in my table where urls are saved, seperated with a , if more than one (i.e www.lala.com,www.hoho.com).

I have a couple of helper functions that are supposed to help me make links, but something's wrong..

application_helper.rb:

def make_link(url = '')
  if url and url != '' and not(url =~ /^http/i)
    url = 'http://' + url
  end
    return '<a href="' + url + '">' + url + '</a>'
end
 
def split_urls(urls)
  @links = urls.split(',')
end

view.rhtml:

<%= split_urls(@news.link)
@links.each do |url|
make_link(url)
end %>

Re: Split and make links

what is it outputting?
are you getting anything out, i.e. is it rendering but tags are wrong; or, is there an actual ruby error, if so, what's the error report saying?

Re: Split and make links

It's outputting the links like so: www.lala.comwww.hoho.com.. so the split seems to work, but the make_link stuff doesn't happen...

Re: Split and make links

I don't know for sure but maybe there is a case of variable names clashing somehow, try replacing your iterator with a new variable name like: -

@links.each do |link|
  make_link(link)
end

Re: Split and make links

Nope, no difference...

Re: Split and make links

very odd, well the http:// append section of your code doesn't seem to be getting called properly, otherwise it wuold be

http://www.foo.comhttphmm/www.bar.com

(had to put that in code since the forum was making it look wierd othrewise)

try changing the first line to

if url != '' and not(url =~ /^http/i)

since the first term will always be true anyway, as you've got a default url='' when passing in the arguments

Last edited by Baltar (2006-08-15 10:44:04)

Re: Split and make links

Yeah, you're right about that.. the make_link functin doesn't get called at all..

Re: Split and make links

I got it right!

def split_urls(urls)
    @links = urls.split(',')
    return @links
end

<% split_urls(@news.links) -%>

<% @links.each do |link| -%>
<%= make_link(link)%>
<%end-%>


seems like the spit had to be done separately, and then looped through.. Thank you for your help Baltar!

Re: Split and make links

glad to hear you got it working, happy to help smile