Frage

I'm currently using the ruby-bitly gem to shorten links in a Rails app; however, the result has an "http://" in front of the bit.ly link. This is not friendly for tweets/other length sensitive posts. How do I automatically remove that?

Link controller action:

 def shorten_with_bitly(url)
    link = Link.find(params[:id]) 
    bitly = Bitly.shorten(url, "MY_ID", "MY_API_KEY")
    bitly.url = link.shortened_link
    link.save 
 end

Thank you very much for your help!!!

War es hilfreich?

Lösung

Use gsub, for instance:

"http://google.com".gsub("http://", "")

or use slice:

"http://google.com".slice!(0..6) 

Keep in mind that the ! will modify the original string, not just give you a modified copy.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top